<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="华为2021软件精英挑战赛复赛赛后方案分享, 大虫のBlog">
    <meta name="description" content="电子科技大学 | 信软学院 | 软件工程">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>华为2021软件精英挑战赛复赛赛后方案分享 | 大虫のBlog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.2.0"></head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">大虫のBlog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fa fa-list" style="zoom: 0.6;"></i>
      
      <span>其他</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a target="_blank" rel="noopener" href="https://console.leancloud.cn">
          
          <i class="fa-solid fa-link" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>LeanCloud 应用</span>
        </a>
      </li>
      
      <li>
        <a target="_blank" rel="noopener" href="https://www.tidio.com/panel">
          
          <i class="fa-solid fa-link" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>TIDIO 聊天系统</span>
        </a>
      </li>
      
      <li>
        <a target="_blank" rel="noopener" href="https://www.coderutil.com">
          
          <i class="fa-solid fa-link" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>CoderUtil 工具</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">大虫のBlog</div>
        <div class="logo-desc">
            
            电子科技大学 | 信软学院 | 软件工程
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fa fa-list"></i>
			
			其他
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a target="_blank" rel="noopener" href="https://console.leancloud.cn " style="margin-left:75px">
				  
				   <i class="fa fa-solid fa-link" style="position: absolute;left:50px" ></i>
			      
		          <span>LeanCloud 应用</span>
                  </a>
                </li>
              
                <li>

                  <a target="_blank" rel="noopener" href="https://www.tidio.com/panel " style="margin-left:75px">
				  
				   <i class="fa fa-solid fa-link" style="position: absolute;left:50px" ></i>
			      
		          <span>TIDIO 聊天系统</span>
                  </a>
                </li>
              
                <li>

                  <a target="_blank" rel="noopener" href="https://www.coderutil.com " style="margin-left:75px">
				  
				   <i class="fa fa-solid fa-link" style="position: absolute;left:50px" ></i>
			      
		          <span>CoderUtil 工具</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://gitee.com/yczlab" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://gitee.com/yczlab" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/22.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">华为2021软件精英挑战赛复赛赛后方案分享</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/%E6%AF%94%E8%B5%9B/">
                                <span class="chip bg-color">比赛</span>
                            </a>
                        
                            <a href="/tags/C/">
                                <span class="chip bg-color">C++</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E6%AF%94%E8%B5%9B/" class="post-category">
                                比赛
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-04-20
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-07-31
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    13.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    57 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>  </p>
<h2 id="引言："><a href="#引言：" class="headerlink" title="引言："></a>引言：</h2><blockquote>
<p>我是来自成渝赛区UESTC的选手，成渝赛区初赛排名13名，复赛最终排名12，再一次成功拿到手环。成渝赛区总报名人数全国第二，电子科技大学单校报名人数全国第一，太卷了，太卷了。鄙人十分不幸，生在成渝赛区的电子科技大学，据说成渝赛区的排名若是放在其他赛区可以有飞一般的提升，哇，我哭得好大声。<img src="/posts/338d62e1/1.png" alt="初赛正式赛排名"> <img src="/posts/338d62e1/2.png" alt="复赛正式赛排名">这里做一个复赛的赛后分享吧，对于前排大佬来说没啥用处，仅供后排抱团取暖，菜鸡互学。gitee仓库开源地址</p>
</blockquote>
<h3 id="赛题介绍"><a href="#赛题介绍" class="headerlink" title="赛题介绍"></a>赛题介绍</h3><p>  云上资源的规划和调度是云计算场景中非常重要的一个优化问题。好的优化算法能够为云运营商节约上亿的运营成本，并为客户提供更稳定、更流畅的云端体验。本次比赛主要是根据用户每天的添加、删除虚拟机请求序列，为云运营商设计总体花费成本最低的每天的迁移虚拟机、部署虚拟机、购买服务器方案，解决用户的请求。初赛时知晓用户所有天的请求序列；而复赛练习赛时为了贴合实际情况，赛题修改为只知晓后续某个固定时间窗口的请求序列，不知道所有天数的请求序列。（这个地方对于我们的代码来说，影响不大，因为我们都是一天天的处理当天请求，没有考虑用到后续天数的请求信息。这也是我们菜的原因，但我觉等大部分队伍可能都没有用到这个信息）。最终复赛正式赛时，现场临时增加需求。本来每天的迁移次数是有上限的，但是复赛正式赛时，增加了一个规则，就是允许你选择一天进行虚拟机的大迁移，也就是这一天的迁移次数没有上限要求，但只允许选择一天进行这样的大迁移。<br>  主办方提供了不同的服务器和虚拟机。<strong>服务器</strong>具有不同的型号、规格（CPU核心数，内存大小）、硬件成本、每日耗能成本。每台服务器上都有两个节点，A节点和B节点，A、B节点的拥有的资源（CPU核心数，内存大小）各占总资源的一半。比如以型号为 NV603 的服务器为例，其 A、B 两个节点分别包含 46C 和 162G 的资源，且主办方保证所给的数据中服务器的CPU 核数和内存大小均为偶数。硬件成本即为服务器一次性的购买成本；若服务器上当天部署了虚拟机，则当天具有能耗成本；若其上没有虚拟机，则当天不耗能。<strong>虚拟机</strong>具有不同的型号、规格（CPU核心数，内存大小）、部署方式。若虚拟机是单节点部署，则其可部署在服务器的任一A、B节点上；若其为双节点部署，则其需的一半资源部署在A节点上，另一半资源部署在同一服务器的B节点上。当然所部署的虚拟机不能超过服务器上的资源容量。</p>
<p><img src="/posts/338d62e1/3.png" alt="服务器类型"><br><img src="/posts/338d62e1/4.png" alt="虚拟机类型"></p>
<h4 id="资源规划和调度"><a href="#资源规划和调度" class="headerlink" title="资源规划和调度"></a>资源规划和调度</h4><ul>
<li><strong>容量约束：</strong>服务器可以用来容纳用户的虚拟机，但是服务器上的任意一个节点(A和 B)上的资源负载(CPU 和内存)均不能超过其容量上限。</li>
<li><strong>请求类型：</strong>用户的请求共分为两种类型：创建请求和删除请求。创建请求表示用户向公有云平台提交的一个创建虚拟机的请求；删除请求表示用户提交的删除一台之前创建的虚拟机的请求。</li>
<li><strong>请求序列：</strong>由一系列请求构成的序列。题目会给出接下来若干天中每一天用户的请求序列，根据每天的请求序列，你需要进行相应的资源规划和调度。</li>
<li><strong>数据中心扩容：</strong>在得知了一天的请求序列后，你可以在实际进行调度前进行一次数据中心扩容。即购买一些新的服务器来容纳后续用户请求的虚拟机，同时你需要付出所购买服务器相应的硬件成本。你需要指定购买哪些类型的服务器以及购买的数量。初始时你没有任何服务器。</li>
<li><strong>虚拟机迁移：</strong>在完成扩容后，在处理每一天的新请求之前，你还可以对当前存量虚拟机进行一次迁移，即把虚拟机从一台服务器迁移至另一台服务器。对于单节点部署的虚拟机，将其从一台服务器的 A 节点迁移至 B 节点(或反之)也是允许的。迁移的目的服务器和节点必须有足够的资源容纳所迁移的虚拟机。迁移的虚拟机总量不超过当前存量虚拟机数量的千分之五。即假设当前有 n 台存量虚拟机，每天你可以迁移的虚拟机总量不得超过 5n&#x2F;10000 向下取整。初赛时这个上限是 5n&#x2F;10000，复赛时这个上限提高到了3n&#x2F;100，也就是说每日可迁移的虚拟机数量提高了。</li>
<li><strong>部署虚拟机：</strong>在完成扩容和迁移之后，你需要按顺序处理当天所有的新请求。对于每一个创建虚拟机的新请求，你要为虚拟机指定一台服务器进行部署。若虚拟机是单节点部署的，你还需要指明部署在服务器的 A 节点还是 B 节点。处理请求的过程中，任意一台服务器上每个节点容纳的虚拟机资源总和都不能超出节点本身的资源容量（指 CPU 和内存两个维度）。</li>
<li><strong>未知请求序列：</strong>在初赛中，我们面对的是提前知晓了未来所有用户请求序列的场景，这在实际中是很难作到的。现实场景中，我们往往只能预测后续较短的一段时间内用户可能的请求序列。所以在复赛中，你需要面对这种存在未知的场景，进行云上的资源规划和调度。</li>
</ul>
<h4 id="输入示例："><a href="#输入示例：" class="headerlink" title="输入示例："></a>输入示例：</h4><p><img src="/posts/338d62e1/5.png" alt="输入示例"></p>
<blockquote>
<ul>
<li>上述示例中，第 1 行表示共有两种类型的服务器可供采购，第 2 和第 3 行分别描述这两种类型服务器的详细信息。</li>
<li>第 4 行表示共有两种类型的虚拟机提供给用户购买，第 5 和第 6 行分别描述这两种类型的虚拟机的详细信息。</li>
<li>第 7 行表示共有三天的用户请求序列。</li>
<li>第 8 行表示第一天的用户请求序列中共有两条请求，第 9 和第 10 行分别描述这两条请求。</li>
<li>第 11-13 行描述第二天的用户请求。</li>
<li>第 14-17 行描述第三天的用户请求。</li>
</ul>
</blockquote>
<h4 id="输出示例："><a href="#输出示例：" class="headerlink" title="输出示例："></a>输出示例：</h4><p><img src="/posts/338d62e1/6.png" alt="输出示例"></p>
<blockquote>
<ul>
<li>上述示例中，第 1 行表示在第一天购买两种类型的服务器，第 2 和第 3 行分别表示购买的服务器型号和数量。</li>
<li>第 4 行表示迁移 0 台虚拟机。若有迁移，则格式为**(虚拟机 ID, 目的服务器 ID)<strong>或</strong>(虚拟机 ID, 目的服务器 ID, 目的服务器节点)**。例如(3, 1)表示将 ID 为 3 的虚拟机从当前所在服务器迁移至 ID 为 1 的服务器，该虚拟机必须是双节点部署的；(4, 1, A)表示将 ID 为 4 的虚拟机从当前所在服务器迁移至 ID 为 1 的服务器的 A 节点，该虚拟机必须是单节点部署的。</li>
<li>第 5 和第 6 行表示第一天创建的两个虚拟机分别部署到服务器 0 的 A 节点和服务器 0 的 B 节点。</li>
<li>第 7-12 行描述第二天的决策信息</li>
</ul>
</blockquote>
<h3 id="总体处理流程"><a href="#总体处理流程" class="headerlink" title="总体处理流程"></a>总体处理流程</h3><p><strong>变量数据结构定义（c++实现）</strong></p>
<pre class="language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//服务器结构</span>
<span class="token keyword">struct</span> <span class="token class-name">Server</span><span class="token punctuation">&#123;</span>
	string type<span class="token punctuation">;</span>
	<span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> memSize<span class="token punctuation">;</span><span class="token comment">//这个值保持不变</span>
	<span class="token keyword">int</span> cpuA<span class="token punctuation">,</span> cpuB<span class="token punctuation">,</span> memA<span class="token punctuation">,</span> memB<span class="token punctuation">,</span> serverCost<span class="token punctuation">,</span> powerCost<span class="token punctuation">;</span>
	<span class="token keyword">int</span> rest<span class="token punctuation">;</span>
	<span class="token comment">//空参构造器</span>
	<span class="token function">Server</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token comment">//带参构造器</span>
	<span class="token function">Server</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> <span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> <span class="token keyword">int</span> memSize<span class="token punctuation">,</span> <span class="token keyword">int</span> sCost<span class="token punctuation">,</span> <span class="token keyword">int</span> pCost<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">cpuCore</span><span class="token punctuation">(</span>cpuCore<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">memSize</span><span class="token punctuation">(</span>memSize<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">serverCost</span><span class="token punctuation">(</span>sCost<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">powerCost</span><span class="token punctuation">(</span>pCost<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>cpuA <span class="token operator">=</span> cpuCore <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>cpuB <span class="token operator">=</span> <span class="token keyword">this</span><span class="token operator">-></span>cpuA<span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>memA <span class="token operator">=</span> memSize <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>memB <span class="token operator">=</span> <span class="token keyword">this</span><span class="token operator">-></span>memA<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token comment">//虚拟机结构</span>
<span class="token keyword">struct</span> <span class="token class-name">VirtualMachine</span><span class="token punctuation">&#123;</span>
	string type<span class="token punctuation">;</span>
	<span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> memSize<span class="token punctuation">,</span> isDual<span class="token punctuation">;</span>
	<span class="token comment">//空参构造器</span>
	<span class="token function">VirtualMachine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token comment">//带参构造器</span>
	<span class="token function">VirtualMachine</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> <span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> <span class="token keyword">int</span> memSize<span class="token punctuation">,</span> <span class="token keyword">int</span> isDual<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">cpuCore</span><span class="token punctuation">(</span>cpuCore<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">memSize</span><span class="token punctuation">(</span>memSize<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">isDual</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token comment">//请求结构</span>
<span class="token keyword">struct</span> <span class="token class-name">Request</span><span class="token punctuation">&#123;</span>
	string type<span class="token punctuation">;</span><span class="token comment">//add or del</span>
	string vmType<span class="token punctuation">,</span> vmId<span class="token punctuation">;</span>
	<span class="token keyword">int</span> vmCpu<span class="token punctuation">,</span> vmMem<span class="token punctuation">,</span> isDual<span class="token punctuation">;</span>
	<span class="token function">Request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token comment">//空参构造器</span>
	<span class="token function">Request</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> string vmType<span class="token punctuation">,</span> string vmId<span class="token punctuation">,</span> <span class="token keyword">int</span> vmCpu<span class="token punctuation">,</span> <span class="token keyword">int</span> vmMem<span class="token punctuation">,</span> <span class="token keyword">int</span> isDual<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmType</span><span class="token punctuation">(</span>vmType<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmId</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmCpu</span><span class="token punctuation">(</span>vmCpu<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmMem</span><span class="token punctuation">(</span>vmMem<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">isDual</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token comment">//当其为一条删除请求时，只有其vmId有效，其他的数据别访问，会出错。</span>
	<span class="token function">Request</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> string vmId<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmId</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token comment">/******* 全局变量 ***********************/</span>
<span class="token keyword">int</span> reqDays<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> windowDays<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
unordered_map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> Server<span class="token operator">></span> svInfos<span class="token punctuation">;</span>              <span class="token comment">//原始的服务器信息</span>
unordered_map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> VirtualMachine<span class="token operator">></span> vmInfos<span class="token punctuation">;</span>      <span class="token comment">//原始的虚拟机信息</span>
vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> svCostServers<span class="token punctuation">;</span>                       <span class="token comment">//按硬件价格排序服务器（小 --> 大）</span>
vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> pwCostServers<span class="token punctuation">;</span>                       <span class="token comment">//按每日能耗排序服务器（小 --> 大）</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>Request<span class="token operator">>></span> daysReqs<span class="token punctuation">;</span>                   <span class="token comment">//所有的请求</span>

vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> svResources<span class="token punctuation">;</span>                         <span class="token comment">//购买的服务器</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">DeployInfo</span> <span class="token expression">vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span>                      </span><span class="token comment">//虚拟机的部署信息。&#123;服务器编号, 占用cpu, 占用mem, maybe 部署节点&#125;。长度为3->双节点部署;当长度为4->单节点部署（0->A, 1->B）</span></span>
unordered_map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> DeployInfo<span class="token operator">></span> vmDeployInfos<span class="token punctuation">;</span>    <span class="token comment">//记录虚拟机运行在哪台服务器上(key=虚拟机id(不是类型), value=&#123;服务器编号,占用cpu,占用mem,部署节点&#125;)</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> svRunVms<span class="token punctuation">;</span>                    <span class="token comment">//记录已购的每台服务器上的各自运行的虚拟机Id。长度和已购服务器的数量对应</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> svRunVmsNumber<span class="token punctuation">;</span>                         <span class="token comment">//记录已购的每台服务器上的各自运行的虚拟机数量（看似显得多余，其实是为了方便后续的处理）。长度和已购服务器的数量对应</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> purchase_info<span class="token punctuation">;</span><span class="token comment">//每天的购买信息</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> migrate_info<span class="token punctuation">;</span><span class="token comment">//每天的迁移信息</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> deploy_info<span class="token punctuation">;</span><span class="token comment">//每天的部署信息</span></code></pre>
<p>  先读取所有的数据，构建所有的服务器和虚拟机信息（<strong>svInfos</strong>、<strong>vmInfos</strong>），并将服务器按照硬件价格和每日耗能排序（<strong>svCostServer</strong>、<strong>pwCostServer</strong>）。然后依次读入所有天的请求，存入 <strong>daysReqs</strong> 中（复赛中不能读入所有天的数据，只能读到指定时间窗口的后续数据），后续可以通过 <strong>dayReqs[day]</strong> 从中取出指定天的请求序列数据。数据处理完成后，便开始依次处理每天的请求序列（包括添加虚拟机和删除虚拟机）。我们队伍的思路是，每天开始处理前，先将当天的请求序列排个序，按照虚拟机所需的资源总量（cpu + mem）从大到小排序，而且把单双节点请求分开，虚拟机核内比（cpu&#x2F;mem）太小或太大的都单独考虑，而且还有一个很重的就是这个排序是分段排序，以删除请求分段。所以在每一段内的顺序是：单节点请求在前面，然后是双节点请求，最后是核内比很奇葩排在最后。分段排序是为了保证删除请求所在序列中的位置固定不变，避免后面处理时出现错误，因为某个已部署的虚拟机删除后，其腾出来的资源就可以部署其他的虚拟机了。如果你问我<strong>为什么要把单节点、双节点、奇葩节点单独弄出来排序？而且单节点还得放前面，奇葩节点放后面？</strong>那我只能说，这是我们调参调出来的结果，确实这样做对于我们的策略来说最后的成本最低。代码中还有一些地方也是用了一些 magic number，不要惊讶，都是一步步调参调出来的。<br>  排序完成后，便可以正式开始处理当天的请求了。顺序遍历排序后的虚拟机请求，若其为删除请求，那就找到其之前部署到的服务器，删除其部署信息，并修改服务器的剩余资源即可。若其为添加请求，则遍历目前所购买的服务器，判断该虚拟机是否能部署到该服务器上，后面会说如何判断。若能部署，则直接部署到该服务器上即可，即修改对应服务器上的剩余资源量，记录该虚拟机的一些部署信息。若已购买的服务器上都不能部署该虚拟机请求，则把该虚拟机请求记录下来，后续统一为所有不能部署的虚拟机购买服务器进行单独的部署，也就是这部分记录下来的暂时无法部署的虚拟机会全部部署到当日新购买的服务器上。置于这个购买的策略后面会讲到，其实也不难，我们选择的是一个贪心购买的策略。因为试了很多策略，发现这个策略对于我们来说最好。当然购买时还有一个很神奇的骚操作，后面再讲吧。<br>  以上讲了我们的部署和购买的过程，其实还有个很重要的迁移过程。赛题要求是，先购买，在迁移，在部署。我们队伍将迁移操作放到了最前面，即不管三七二十一，每天一上来先迁移，尽量腾空服务器出来，这样可以减少耗能成本，然后再部署当日请求，购买服务器部署剩余请求。但是我们必须得考虑输出信息的顺序，因为这点也很重要，按要求，先输出购买信息，再输出迁移信息，最后输出部署信息。只要保证这个输出的顺序是正确的就行。有人可能会问，我先迁移后，后面部署时会把本来腾空的服务器又占用了，岂不是还是会有耗能成本。这点确实是这样，但是，如果是先部署再迁移，那么根据题意要求，迁移时不能考虑新部署的虚拟机，因为赛题要求迁移时考虑的虚拟机是当前存量的虚拟机，那么这样考虑的东西变多了，而且也不好处理输出，我们想的是怎么简单怎么来。所以就把迁移直接放到了每一天的开始，这样的话，当前存量的虚拟机就是当前已部署的虚拟机，没有什么歧义；而且我们发现所提供的练习数据中，后面的很多天中的请求几乎都是删除请求，也就是说，在后面几天里，迁移之后，本来腾空的虚拟机当天也基本不怎么会有虚拟机再部署上来了，达到降低能耗成本的目的。这也是我们选择先迁移的整个心路历程。至于如何进行迁移，哪台虚拟机该迁移到哪台服务器上，请听后续的分解。这里只是给出了整个流程：<strong>排序，迁移，部署，购买部署。</strong></p>
<h3 id="部署策略"><a href="#部署策略" class="headerlink" title="部署策略"></a>部署策略</h3><p>  当一个添加虚拟机的请求到来时，如何判断用哪个已购买的服务器去部署这个虚拟机呢。这里我们队伍做了很多尝试，比如先将已购的服务器排个序，按照服务器上的剩余资源量（A、B节点剩余cpu和mem的总量和）从小到大排序，然后找到第一个能装下该虚拟机的服务器，就将该虚拟机部署到该服务器上。这样做，确实也是最简单的最容易想到的方法，但是效果不咋地。我们还尝试了一些其他的方法，这里就不一一说明了，直接说一说我们最终的方案。不用对已买服务器进行排序，而是直接挨个遍历所以已买服务器，找到该虚拟机能部署到的（即满足资源要求）且部署之后该服务器剩余资源最小，且部署后该服务器A、B节点不失衡的服务器进行部署，如果找不到这样服务器，则把这个虚拟机请求记录下来，后续统一进行购买部署处理。一开始我们其实并没有想这么多，只想到了部署之后若该服务器剩余资源最小，那么我们就部署到该服务器上。但后来我们考虑了平衡部署，也就是说如果这个虚拟机部署到这个服务器上后，这个服务器上剩余的CPU资源和mem资源比例失衡了，那我们就不要部署到这样的服务器上。这个失衡的比例我们设置成了 <strong>ratio&lt;0.13 或 ratio&gt;7.5</strong> 都算作是失衡了，反正这个参数我们调了很久才调出来一个比较满意的值（其中ratio &#x3D; 剩余CPU&#x2F;剩余MEM）。这个过程讲出来看似很简单，其实实现起来考虑的东西还是比较多的，因为要考虑虚拟机的单双节点不同部署方式的差异和服务器A、B资源不同剩余量的情况。所以就是一两句话也说不清楚，但我们确实是考虑了平衡部署后，效果有了很大的提升。具体步骤还得仔细参考我们的代码中 <strong>findBestServer()</strong> 这个函数。这个比赛主要不是比时间，而是看最后谁提供的方案成本更低，所以可以疯狂遍历，并且我们发现，这个遍历也花费不了所少时间。比赛要求在90内得出结果，我们的时间花费排名应该算是比较靠前的，相比于其他队伍。所以时间并不是阻碍我们上分的主要因素，哎，还是方案太菜了，进不了决赛。具体细节请参考我们代码中的 <strong>handleOneDayRequests()函数</strong>。</p>
<h3 id="购买策略"><a href="#购买策略" class="headerlink" title="购买策略"></a>购买策略</h3><p>  前面说到，我们会统一考虑为部署不了的虚拟机购买服务器。我们用一个临时变量专门记录当天新买的服务器，然后做好编号映射（为了满足输出要求，据说前期很多队伍掉到了这个坑里，没有做编号映射。比如你买的时候是按ABCABC这些类型买的，那么你默认他们的编号依次是123456，但是赛题要求的输出是AABBCC，编号依次是123456，这个时候你若是没有做好编号映射，判题系统就会给你判错）后更新到全局的已购服务器列表中。我们依次遍历收集到的前期无法部署的虚拟机，看当天新买的服务器中是否有服务器能部署上这台虚拟机，这个判断标准类似于上面部署策略中使用的 <strong>findBestServer()</strong> 函数，但是在平衡部署的判断上略有不同，所以我们单独又写了一个 <strong>findBestServer()</strong> 函数来在这里做判断。如果在当日新买的服务器中能找到能部署的服务器，那也就可以直接部署了；如果找不到能部署的服务器，那就以这个虚拟机请求为准去考虑如何购买服务器来。前面我们将服务器种类按照硬件价格和每日耗能排过序（<strong>svCostServer</strong>、<strong>pwCostServer</strong>）。我们在购买时，设定了一个算是超参数的东西，一个天数 <strong>T &#x3D; 333</strong>。当前处理的请求天数低于这个参数值时，我们按照每日耗能排序去购买，当前处理的请求天数高于这个参数值时，我们按照硬件价格排序去购买。为什么，大家可以想一想，其实也很简单。你先买的服务器毕竟要一直用到最后一天，得让他们的耗能低才好；后面买的服务器耗电可能就不是主要考虑因素了，怎么买便宜就怎么来。置于为什么我们将这个值设置成了 333，那也是调参调出来的结果，所以这玩意就很神奇。当我们购买时，其实也并不是说只要这个服务器能放下这个虚拟机就购买，我们还是得为后续的虚拟机稍微考虑一下，所以我们购买时规定这台服务器要能装下 <strong>1.32倍</strong> 的该虚拟机资源才购买；如果所有的都不能放下 <strong>1.32倍</strong>，那就降低一点设为 <strong>1.2倍</strong>；如果还是不能放下，那就回归本质到 <strong>1倍</strong>，主办方已说明每台虚拟机至少有一台服务器能装下。初赛时我们就是靠着这个倍数购买上的分，<strong>在最后千钧一发之际，从三十几名飙到了十几名，成功进入复赛</strong>。这个想法也是我们队员提出来的，感觉还挺神奇的，估计很多队伍都没敢这么去尝试，都想着最低能装下该虚拟机就行了，但确实考虑倍数购买效果提升很大，至少对于我们的这份代码来说，提升很大。直接决定了我们能不能进复赛。具体细节请参考我们代码中的 <strong>buyServersForReqs()函数</strong>。</p>
<h3 id="迁移策略"><a href="#迁移策略" class="headerlink" title="迁移策略"></a>迁移策略</h3><p>  迁移这块也是很复杂的，我们前面也讲到了我们队伍将迁移拿到了最前面进行处理。每日的虚拟机迁移次数是有上限的，且复赛和初赛中这个上限还不一样，复赛中提高了这个上限值，允许迁移的次数变多了。首先我们将当前已购买的服务器进行了排序，按照其上剩余的资源量从大到小进行排序，第二序为耗电成本，整体迁移方向是<strong>剩余资源量大的服务器上的虚拟机往剩余资源量小的服务器上迁移</strong>，尽量多的空出服务器来，节约耗电成本。如果单考虑这一点，效果不是很好，肯定是进不了复赛的。因为这个迁移的策略对最终的结果影响很大，大于能<strong>节约 2 亿左右</strong>的成本，直接影响你的排名。我们最后的优化，主要就是放在了迁移这块。按照我们一开始的想法，<strong>剩余资源量大的服务器上的虚拟机往剩余资源量小的服务器上迁移</strong>，我们实现了一版出来，每次依次从前面的服务器上依次取出其上部署的虚拟机，往后面能迁移到的服务器且迁移后剩余资源量最少的服务器上迁移，直到到达当日最大迁移次数或不能再迁移。这个方法不仅速度慢，迁移效果还不是很好，达不到最大迁移次数，严重影响效果，而且还经常超时。我们在这个地方卡了很久，掉了很多头发，最终一步步优化，才走出了超时这个坑。最终我们的迁移策略整体方向没有变，但是中间进行了很多优化，使得效果提升。先是一样的，将当前已购买的服务器按剩余资源量从大到小排序，然后我们将今日即将要删除的虚拟机统计了一下（因为我们知道当日的请求），每次依次从前面的服务器上依次取出其上部署的虚拟机。<strong>如果</strong>该虚拟机即将在当天被删除，那么就不考虑当台虚拟机的迁移；<strong>如果</strong>该虚拟机是才迁移到这台服务器上来的，也不考虑该虚拟机的迁移；<strong>如果</strong>该虚拟机无法找到合适的迁移目标服务器，也不迁移；<strong>其他情况</strong>下该虚拟机能正常迁移，动态更新服务器上的剩余资源量。<strong>最重要的一点来了</strong>，如果该台服务器上目前遇到了一台不能迁移的虚拟机，那么这台服务器当天的耗能成本肯定是不能被节省下来了，因为它至少有这么一台不能迁移的虚拟机运行着。那么一旦出现这种情况，我们就记录下该台服务器，并且该台服务器上余下的虚拟机也不考虑去迁移了，直接考虑下一台服务器上虚拟机的迁移。后续我们考虑其他的虚拟机的迁移服务器目标时，将这些记录下来的服务器也当做目标来考虑，因为这些服务器反正是开机状态，不能省电了，那我们就得把它利用起来。<strong>还有一点重要的就是，</strong>如果这台服务器上的装载的资源很满，那其上的虚拟机也就不考虑迁移了，我们用一个衡量标准<strong>（资源剩余量 &#x2F; 资源总容量 &lt; 0.018）</strong>来判断装载率。我们一点点实现了上面的所有想法，最后发现确实效果不错，时间大大缩短了，效果大大增加了，不过这个过程确实很复杂，还有很多细节需要处理，实现过程中又掉了很多头发。具体细节请参考我们代码中的 <strong>migrate()函数</strong>。</p>
<h3 id="赛后感受"><a href="#赛后感受" class="headerlink" title="赛后感受"></a>赛后感受</h3><ul>
<li>个人还是比较喜欢该类比赛，能为企业解决实际问题</li>
<li>细节决定成败，代码编写还是要规范，命名也最好注意一下，简明扼要</li>
<li>有队友还是好得多，不用孤军奋战</li>
<li>一个好赛区也很重要，成渝赛区简直不要太卷，但是莫得办法</li>
<li>思想碰撞，交流沟通也很重要</li>
<li>快速搭建出框架，后续不断优化上分</li>
</ul>
<h3 id="开源代码"><a href="#开源代码" class="headerlink" title="开源代码"></a>开源代码</h3><p>仅供参考，确实有点复杂，若没有参加比赛理解题意，很难看懂。一下是复赛练习赛时我们队伍的最终代码，也可访问我的 <a target="_blank" rel="noopener" href="https://gitee.com/yczlab/hua-wei_2021">开源仓库</a>，查看所有版本的代码。</p>
<pre class="language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unordered_map></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;queue></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;set></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;vector></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;ctime></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;algorithm></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;assert.h></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;math.h></span></span>

<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token comment">//服务器结构</span>
<span class="token keyword">struct</span> <span class="token class-name">Server</span><span class="token punctuation">&#123;</span>
	string type<span class="token punctuation">;</span>
	<span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> memSize<span class="token punctuation">;</span><span class="token comment">//这个值保持不变</span>
	<span class="token keyword">int</span> cpuA<span class="token punctuation">,</span> cpuB<span class="token punctuation">,</span> memA<span class="token punctuation">,</span> memB<span class="token punctuation">,</span> serverCost<span class="token punctuation">,</span> powerCost<span class="token punctuation">;</span>
	<span class="token keyword">int</span> rest<span class="token punctuation">;</span>
	<span class="token comment">//空参构造器</span>
	<span class="token function">Server</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token comment">//带参构造器</span>
	<span class="token function">Server</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> <span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> <span class="token keyword">int</span> memSize<span class="token punctuation">,</span> <span class="token keyword">int</span> sCost<span class="token punctuation">,</span> <span class="token keyword">int</span> pCost<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">cpuCore</span><span class="token punctuation">(</span>cpuCore<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">memSize</span><span class="token punctuation">(</span>memSize<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">serverCost</span><span class="token punctuation">(</span>sCost<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">powerCost</span><span class="token punctuation">(</span>pCost<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>cpuA <span class="token operator">=</span> cpuCore <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>cpuB <span class="token operator">=</span> <span class="token keyword">this</span><span class="token operator">-></span>cpuA<span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>memA <span class="token operator">=</span> memSize <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token operator">-></span>memB <span class="token operator">=</span> <span class="token keyword">this</span><span class="token operator">-></span>memA<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token comment">//虚拟机结构</span>
<span class="token keyword">struct</span> <span class="token class-name">VirtualMachine</span><span class="token punctuation">&#123;</span>
	string type<span class="token punctuation">;</span>
	<span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> memSize<span class="token punctuation">,</span> isDual<span class="token punctuation">;</span>
	<span class="token comment">//空参构造器</span>
	<span class="token function">VirtualMachine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token comment">//带参构造器</span>
	<span class="token function">VirtualMachine</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> <span class="token keyword">int</span> cpuCore<span class="token punctuation">,</span> <span class="token keyword">int</span> memSize<span class="token punctuation">,</span> <span class="token keyword">int</span> isDual<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">cpuCore</span><span class="token punctuation">(</span>cpuCore<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">memSize</span><span class="token punctuation">(</span>memSize<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">isDual</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token comment">//请求结构</span>
<span class="token keyword">struct</span> <span class="token class-name">Request</span><span class="token punctuation">&#123;</span>
	string type<span class="token punctuation">;</span><span class="token comment">//add or del</span>
	string vmType<span class="token punctuation">,</span> vmId<span class="token punctuation">;</span>
	<span class="token keyword">int</span> vmCpu<span class="token punctuation">,</span> vmMem<span class="token punctuation">,</span> isDual<span class="token punctuation">;</span>
	<span class="token function">Request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token comment">//空参构造器</span>
	<span class="token function">Request</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> string vmType<span class="token punctuation">,</span> string vmId<span class="token punctuation">,</span> <span class="token keyword">int</span> vmCpu<span class="token punctuation">,</span> <span class="token keyword">int</span> vmMem<span class="token punctuation">,</span> <span class="token keyword">int</span> isDual<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmType</span><span class="token punctuation">(</span>vmType<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmId</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmCpu</span><span class="token punctuation">(</span>vmCpu<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmMem</span><span class="token punctuation">(</span>vmMem<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">isDual</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
	<span class="token comment">//当其为一条删除请求时，只有其vmId有效，其他的数据别访问，会出错。</span>
	<span class="token function">Request</span><span class="token punctuation">(</span>string type<span class="token punctuation">,</span> string vmId<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">type</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">vmId</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

<span class="token comment">/******* 全局变量 ***********************/</span>
<span class="token keyword">int</span> reqDays<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> windowDays<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
unordered_map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> Server<span class="token operator">></span> svInfos<span class="token punctuation">;</span>              <span class="token comment">//原始的服务器信息</span>
unordered_map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> VirtualMachine<span class="token operator">></span> vmInfos<span class="token punctuation">;</span>      <span class="token comment">//原始的虚拟机信息</span>
vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> svCostServers<span class="token punctuation">;</span>                       <span class="token comment">//按硬件价格排序服务器（小 --> 大）</span>
vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> pwCostServers<span class="token punctuation">;</span>                       <span class="token comment">//按每日能耗排序服务器（小 --> 大）</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>Request<span class="token operator">>></span> daysReqs<span class="token punctuation">;</span>                   <span class="token comment">//所有的请求</span>

vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> svResources<span class="token punctuation">;</span>                         <span class="token comment">//购买的服务器</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">DeployInfo</span> <span class="token expression">vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span>                      </span><span class="token comment">//虚拟机的部署信息。&#123;服务器编号, 占用cpu, 占用mem, maybe 部署节点&#125;。长度为3->双节点部署;当长度为4->单节点部署（0-A,1->B）</span></span>
unordered_map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> DeployInfo<span class="token operator">></span> vmDeployInfos<span class="token punctuation">;</span>    <span class="token comment">//记录虚拟机运行在哪台服务器上(key=虚拟机id(不是类型), value=&#123;服务器编号,占用cpu,占用mem,部署节点&#125;)</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> svRunVms<span class="token punctuation">;</span>                    <span class="token comment">//记录已购的每台服务器上的各自运行的虚拟机Id。长度和已购服务器的数量对应</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> svRunVmsNumber<span class="token punctuation">;</span>                         <span class="token comment">//记录已购的每台服务器上的各自运行的虚拟机数量（看似显得多余，其实是为了方便后续的处理）。长度和已购服务器的数量对应</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> purchase_info<span class="token punctuation">;</span><span class="token comment">//每天的购买信息</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> migrate_info<span class="token punctuation">;</span><span class="token comment">//每天的迁移信息</span>
vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> deploy_info<span class="token punctuation">;</span><span class="token comment">//每天的部署信息</span>
<span class="token comment">//提交时可能用不到的变量</span>
<span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">define</span> <span class="token macro-name">INPUT_REDIRECTION_1</span> <span class="token string">"training-data/training-1.txt"</span>  <span class="token comment">//输入重定向</span></span>
<span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">define</span> <span class="token macro-name">INPUT_REDIRECTION_2</span> <span class="token string">"training-data/training-2.txt"</span>  <span class="token comment">//输入重定向</span></span>
<span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">define</span> <span class="token macro-name">OUTPUT_REDIRECTION</span> <span class="token string">"result.txt"</span>                     <span class="token comment">//输出重定向</span></span>
<span class="token keyword">long</span> <span class="token keyword">long</span> SERVERCOST<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> POWERCOST<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> TOTALCOST<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>            <span class="token comment">//各种成本</span>
<span class="token keyword">int</span> MIGRATE_NUMBER<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> TOTAL_MIGRATE<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>                       <span class="token comment">//迁移数量</span>
<span class="token keyword">long</span> <span class="token keyword">long</span> SC<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> PC<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> TC<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>                                  <span class="token comment">//两份数据的成本</span>
<span class="token comment">/************************************************/</span>

<span class="token comment">/****************** 读数据构建各种类型的 服务器 和 虚拟机 ********************/</span>
<span class="token keyword">void</span> <span class="token function">readServer</span><span class="token punctuation">(</span>string <span class="token operator">&amp;</span>serverType<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>cpuCore<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>memSize<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>serverCost<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>powerCost<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	string _serverType <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>serverType<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _serverType <span class="token operator">+=</span> serverType<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> _cpuCore<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> _memSize<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> _serverCost<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> _powerCost<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>cpuCore<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _cpuCore <span class="token operator">=</span> <span class="token number">10</span><span class="token operator">*</span>_cpuCore <span class="token operator">+</span> cpuCore<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>memSize<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _memSize <span class="token operator">=</span> <span class="token number">10</span><span class="token operator">*</span>_memSize <span class="token operator">+</span> memSize<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>serverCost<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _serverCost <span class="token operator">=</span> <span class="token number">10</span><span class="token operator">*</span>_serverCost <span class="token operator">+</span> serverCost<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>powerCost<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _powerCost <span class="token operator">=</span> <span class="token number">10</span><span class="token operator">*</span>_powerCost <span class="token operator">+</span> powerCost<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span>
	svInfos<span class="token punctuation">[</span>_serverType<span class="token punctuation">]</span> <span class="token operator">=</span> Server<span class="token punctuation">&#123;</span>_serverType<span class="token punctuation">,</span> _cpuCore<span class="token punctuation">,</span> _memSize<span class="token punctuation">,</span> _serverCost<span class="token punctuation">,</span> _powerCost<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">readVm</span><span class="token punctuation">(</span>string <span class="token operator">&amp;</span>vmType<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>cpuCore<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>memSize<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    string _vmType <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>vmType<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _vmType <span class="token operator">+=</span> vmType<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> _cpuCore<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> _memSize<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> _isDual<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>cpuCore<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _cpuCore <span class="token operator">=</span> _cpuCore<span class="token operator">*</span><span class="token number">10</span> <span class="token operator">+</span> cpuCore<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>memSize<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> _memSize <span class="token operator">=</span> _memSize<span class="token operator">*</span><span class="token number">10</span> <span class="token operator">+</span> memSize<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>isDual<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token char">'1'</span><span class="token punctuation">)</span> _isDual <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    vmInfos<span class="token punctuation">[</span>_vmType<span class="token punctuation">]</span> <span class="token operator">=</span> VirtualMachine<span class="token punctuation">&#123;</span>_vmType<span class="token punctuation">,</span> _cpuCore<span class="token punctuation">,</span> _memSize<span class="token punctuation">,</span> _isDual<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">readServerAndVmInfos</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> svNum<span class="token punctuation">;</span><span class="token comment">//服务器数量</span>
	string type<span class="token punctuation">,</span> cpuCore<span class="token punctuation">,</span> memSize<span class="token punctuation">,</span> serverCost<span class="token punctuation">,</span> powerCost<span class="token punctuation">,</span> isDual<span class="token punctuation">;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>svNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svNum<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		cin<span class="token operator">>></span>type<span class="token operator">>></span>cpuCore<span class="token operator">>></span>memSize<span class="token operator">>></span>serverCost<span class="token operator">>></span>powerCost<span class="token punctuation">;</span>
		<span class="token function">readServer</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> cpuCore<span class="token punctuation">,</span> memSize<span class="token punctuation">,</span> serverCost<span class="token punctuation">,</span> powerCost<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">int</span> vmNum<span class="token punctuation">;</span><span class="token comment">//虚拟机数量</span>
    <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>vmNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>vmNum<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        cin<span class="token operator">>></span>type<span class="token operator">>></span>cpuCore<span class="token operator">>></span>memSize<span class="token operator">>></span>isDual<span class="token punctuation">;</span>
        <span class="token function">readVm</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> cpuCore<span class="token punctuation">,</span> memSize<span class="token punctuation">,</span> isDual<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token comment">//复制一份排序。</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>pair<span class="token operator">:</span>svInfos<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		svCostServers<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>pair<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span>
		pwCostServers<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>pair<span class="token punctuation">.</span>second<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token comment">//硬件价格排序</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>svCostServers<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> svCostServers<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span>Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> Server <span class="token keyword">const</span> s2<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>s1<span class="token punctuation">.</span>serverCost <span class="token operator">==</span> s2<span class="token punctuation">.</span>serverCost<span class="token punctuation">)</span> <span class="token keyword">return</span> s1<span class="token punctuation">.</span>powerCost <span class="token operator">&lt;</span> s2<span class="token punctuation">.</span>powerCost<span class="token punctuation">;</span>
		<span class="token keyword">return</span> s1<span class="token punctuation">.</span>serverCost <span class="token operator">&lt;</span> s2<span class="token punctuation">.</span>serverCost<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//每日能耗排序</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>pwCostServers<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pwCostServers<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span>Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> Server <span class="token keyword">const</span> s2<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>s1<span class="token punctuation">.</span>powerCost<span class="token operator">==</span> s2<span class="token punctuation">.</span>powerCost<span class="token punctuation">)</span> <span class="token keyword">return</span> s1<span class="token punctuation">.</span>serverCost <span class="token operator">&lt;</span> s2<span class="token punctuation">.</span>serverCost<span class="token punctuation">;</span>
		<span class="token keyword">return</span> s1<span class="token punctuation">.</span>powerCost <span class="token operator">&lt;</span> s2<span class="token punctuation">.</span>powerCost<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">/*****************************************************************************/</span>

<span class="token comment">/************** 读数据构建每日请求 ****************/</span>
<span class="token keyword">void</span> <span class="token function">readAddRequest</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>op<span class="token punctuation">,</span> string <span class="token operator">&amp;</span> reqVmType<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>reqVmId<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	string _op<span class="token punctuation">,</span> _reqVmType<span class="token punctuation">,</span> _reqVmId<span class="token punctuation">;</span>
    _op <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> op<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    _reqVmType <span class="token operator">=</span> reqVmType<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> reqVmType<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    _reqVmId <span class="token operator">=</span> reqVmId<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> reqVmId<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    VirtualMachine vm <span class="token operator">=</span> vmInfos<span class="token punctuation">[</span>_reqVmType<span class="token punctuation">]</span><span class="token punctuation">;</span>
    daysReqs<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>Request<span class="token punctuation">&#123;</span>_op<span class="token punctuation">,</span> _reqVmType<span class="token punctuation">,</span> _reqVmId<span class="token punctuation">,</span> vm<span class="token punctuation">.</span>cpuCore<span class="token punctuation">,</span> vm<span class="token punctuation">.</span>memSize<span class="token punctuation">,</span> vm<span class="token punctuation">.</span>isDual<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">readDelRequest</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>op<span class="token punctuation">,</span> string <span class="token operator">&amp;</span>reqVmId<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	string _op<span class="token punctuation">,</span> _reqVmId<span class="token punctuation">;</span>
    _op <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> op<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    _reqVmId <span class="token operator">=</span> reqVmId<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> reqVmId<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    daysReqs<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>Request<span class="token punctuation">&#123;</span>_op<span class="token punctuation">,</span> _reqVmId<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">readDayRequests</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token punctuation">,</span> <span class="token keyword">int</span> dayReqNumber<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	string op<span class="token punctuation">,</span> reqVmType<span class="token punctuation">,</span> reqVmId<span class="token punctuation">;</span>
	daysReqs<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>Request<span class="token operator">></span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>dayReqNumber<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		cin<span class="token operator">>></span>op<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>op<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token char">'a'</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			cin<span class="token operator">>></span>reqVmType<span class="token operator">>></span>reqVmId<span class="token punctuation">;</span>
			<span class="token function">readAddRequest</span><span class="token punctuation">(</span>day<span class="token punctuation">,</span> op<span class="token punctuation">,</span> reqVmType<span class="token punctuation">,</span> reqVmId<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
			cin<span class="token operator">>></span>reqVmId<span class="token punctuation">;</span>
			<span class="token function">readDelRequest</span><span class="token punctuation">(</span>day<span class="token punctuation">,</span> op<span class="token punctuation">,</span> reqVmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">/**************************************************/</span>

<span class="token comment">/*************** 为剩余的虚拟机请求购买服务器并部署 *******/</span>
<span class="token comment">//寻找合适的服务器购买</span>
<span class="token keyword">int</span> <span class="token function">buyBestServer</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>servers<span class="token punctuation">,</span> Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> needCpu <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmCpu<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmCpu<span class="token punctuation">;</span>
	<span class="token keyword">int</span> needMem <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmMem<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span>
	<span class="token comment">//单双节点都是统一的判断方式，很神奇，是吧</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>servers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv<span class="token operator">=</span>servers<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">></span><span class="token number">1.32</span><span class="token operator">*</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">></span><span class="token number">1.32</span><span class="token operator">*</span>needMem<span class="token punctuation">)</span> <span class="token keyword">return</span> i<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>servers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv<span class="token operator">=</span>servers<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">></span><span class="token number">1.2</span><span class="token operator">*</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">></span><span class="token number">1.2</span><span class="token operator">*</span>needMem<span class="token punctuation">)</span> <span class="token keyword">return</span> i<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>servers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv<span class="token operator">=</span>servers<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem<span class="token punctuation">)</span> <span class="token keyword">return</span> i<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//这个地方永远也执行不了</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">int</span> <span class="token function">findBestServer</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>Server<span class="token operator">></span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>servers<span class="token punctuation">,</span> Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token punctuation">,</span> <span class="token keyword">int</span> start<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> minRest <span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">8</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> needCpu <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmCpu<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmCpu<span class="token punctuation">;</span>
	<span class="token keyword">int</span>	needMem <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmMem<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span>
	<span class="token keyword">int</span> rest <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//双节点</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>start<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>servers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv <span class="token operator">=</span> servers<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token comment">//float ratio1 = (float)(sv.cpuA-needCpu)/(sv.memA-needMem);</span>
				<span class="token comment">//if(ratio1&lt;0.13 || ratio1>7.5) continue;//保证A节点都不失衡</span>
				<span class="token keyword">float</span> ratio2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">-</span>needCpu<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>memB<span class="token operator">-</span>needMem<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>ratio2<span class="token operator">&lt;</span><span class="token number">0.13</span> <span class="token operator">||</span> ratio2<span class="token operator">></span><span class="token number">7.5</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//保证B节点都不失衡</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>sv<span class="token punctuation">.</span>memB <span class="token operator">-</span> req<span class="token punctuation">.</span>vmCpu <span class="token operator">-</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span><span class="token comment">//放入后的剩余资源量 cpu + mem</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest <span class="token operator">&lt;</span> minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//单节点</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>start<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>servers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv <span class="token operator">=</span> servers<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token comment">//float ratio1 = (float)(sv.cpuA-needCpu)/(sv.memA-needMem);</span>
				<span class="token comment">//if(ratio1&lt;0.13 || ratio1>7.5) continue;//保证A节点都不失衡</span>
				<span class="token keyword">float</span> ratio2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">-</span>needCpu<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>memB<span class="token operator">-</span>needMem<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>ratio2<span class="token operator">&lt;</span><span class="token number">0.13</span> <span class="token operator">||</span> ratio2<span class="token operator">></span><span class="token number">7.5</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//保证B节点都不失衡</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>sv<span class="token punctuation">.</span>memB <span class="token operator">-</span> req<span class="token punctuation">.</span>vmCpu <span class="token operator">-</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span><span class="token comment">//放入后的剩余资源量 cpu + mem</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest <span class="token operator">&lt;</span> minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> index<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//为一波请求买服务器，并同时部署。这波请求可能含有del</span>
<span class="token keyword">void</span> <span class="token function">buyServersForReqs</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token punctuation">,</span> vector<span class="token operator">&lt;</span>Request<span class="token operator">></span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>reqs<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> old_sv_number <span class="token operator">=</span> svResources<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//新买的服务器从这里开始编号</span>
	unordered_map<span class="token operator">&lt;</span>string<span class="token punctuation">,</span> DeployInfo<span class="token operator">></span> temp_dep_infos<span class="token punctuation">;</span><span class="token comment">//临时的部署信息，最后把这个信息更新到全局部署信息中。这里主要是为了编号映射，太麻烦了</span>

	<span class="token keyword">for</span><span class="token punctuation">(</span>Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token operator">:</span>reqs<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		string vmId <span class="token operator">=</span> req<span class="token punctuation">.</span>vmId<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>type<span class="token operator">==</span><span class="token string">"add"</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//添加请求</span>
			<span class="token keyword">int</span> isDual <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual<span class="token punctuation">;</span>
			<span class="token keyword">int</span> needCpu <span class="token operator">=</span> isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmCpu<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmCpu<span class="token punctuation">;</span>
			<span class="token keyword">int</span> needMem <span class="token operator">=</span> isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmMem<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span>
			<span class="token keyword">int</span> svId <span class="token operator">=</span> <span class="token function">findBestServer</span><span class="token punctuation">(</span>svResources<span class="token punctuation">,</span> req<span class="token punctuation">,</span> old_sv_number<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>svId <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//需购买</span>
				<span class="token keyword">int</span> index<span class="token punctuation">;</span>
				Server sv<span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>day <span class="token operator">&lt;=</span> <span class="token number">333</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//333</span>
					index <span class="token operator">=</span> <span class="token function">buyBestServer</span><span class="token punctuation">(</span>pwCostServers<span class="token punctuation">,</span> req<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//按照每日能耗最低，买一台最合适的服务器</span>
					sv <span class="token operator">=</span> pwCostServers<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//购买</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					index <span class="token operator">=</span> <span class="token function">buyBestServer</span><span class="token punctuation">(</span>svCostServers<span class="token punctuation">,</span> req<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//按照硬件成本最低，买一台最合适的服务器</span>
					sv <span class="token operator">=</span> svCostServers<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//购买</span>
				<span class="token punctuation">&#125;</span>
				SERVERCOST <span class="token operator">+=</span> sv<span class="token punctuation">.</span>serverCost<span class="token punctuation">;</span>
				svId <span class="token operator">=</span> svResources<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//这台新购买的服务器编号</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//双节点</span>
					sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
					temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span><span class="token operator">=</span>DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//单节点，默认放在新买服务器的B节点上</span>
					sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
					temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span><span class="token operator">=</span>DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
				svResources<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>sv<span class="token punctuation">)</span><span class="token punctuation">;</span>
				svRunVmsNumber<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				svRunVms<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>string<span class="token operator">></span><span class="token punctuation">&#123;</span>vmId<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//无需购买即可部署</span>
				Server <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//涉及到修改资源量了</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//双节点部署</span>
					sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
					temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
					svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
					svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//单节点部署</span>
					<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA <span class="token operator">&lt;=</span> sv<span class="token punctuation">.</span>cpuB<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//A节点剩得少</span>
						<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//并且A节点能放下</span>
							sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
							sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
							temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
							svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
							svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
							<span class="token keyword">continue</span><span class="token punctuation">;</span>
						<span class="token punctuation">&#125;</span>
						<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//A节点剩得少，但是A节点放不下</span>
							sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
							sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
							temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
							svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
							svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
							<span class="token keyword">continue</span><span class="token punctuation">;</span>
						<span class="token punctuation">&#125;</span>
					<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//B节点剩得少</span>
						<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//并且B节点能放下</span>
							sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
							sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
							temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
							svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
							svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
							<span class="token keyword">continue</span><span class="token punctuation">;</span>
						<span class="token punctuation">&#125;</span>
						<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//B节点剩得少，但是B节点放不下</span>
							sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
							sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
							temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
							svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
							svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
							<span class="token keyword">continue</span><span class="token punctuation">;</span>
						<span class="token punctuation">&#125;</span>
					<span class="token punctuation">&#125;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//删除请求</span>
			DeployInfo vmDeInfo <span class="token operator">=</span> temp_dep_infos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> svId<span class="token operator">=</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> occupiedCpu<span class="token operator">=</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> occupiedMem<span class="token operator">=</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//获取部署时的关键信息</span>
			Server <span class="token operator">&amp;</span>sv<span class="token operator">=</span>svResources<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//涉及到修改资源量了</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>vmDeInfo<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是双节点部署的</span>
				sv<span class="token punctuation">.</span>cpuA <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>cpuB <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memA <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memB <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是单节点部署的</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是部署在A节点上</span>
					sv<span class="token punctuation">.</span>cpuA <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memA <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					sv<span class="token punctuation">.</span>cpuB <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memB <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token comment">//temp_dep_infos.erase(vmId);//暂时不删，不然做编号映射时会出问题。最后统一删除部署信息</span>
			svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
			vector<span class="token operator">&lt;</span>string<span class="token operator">></span> <span class="token operator">&amp;</span>VMIDS<span class="token operator">=</span>svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span>
			VMIDS<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">remove</span><span class="token punctuation">(</span>VMIDS<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> vmId<span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//没错删除就是这么复杂</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>

	vector<span class="token operator">&lt;</span>string<span class="token operator">></span> day_buy_info<span class="token punctuation">&#123;</span><span class="token string">""</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span class="token comment">//保存购买信息</span>
	set<span class="token operator">&lt;</span>string<span class="token operator">></span> diffSvType<span class="token punctuation">;</span><span class="token comment">//这个东西可以记录买了多少种</span>
	<span class="token comment">//为了编号映射，好球麻烦哟</span>
	unordered_map<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> Server<span class="token operator">></span> server_map<span class="token punctuation">;</span>
	unordered_map<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> vector<span class="token operator">&lt;</span>string<span class="token operator">>></span> sv_run_vms_map<span class="token punctuation">;</span>
	unordered_map<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> svRunVmsNumber_map<span class="token punctuation">;</span>
	unordered_map<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> svId_map<span class="token punctuation">;</span>
	<span class="token keyword">int</span> ID <span class="token operator">=</span> old_sv_number<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>old_sv_number<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svResources<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		string svType<span class="token operator">=</span> svResources<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>type<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>diffSvType<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>svType<span class="token punctuation">)</span><span class="token operator">!=</span>diffSvType<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> number<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment">//每种买了多少</span>
		diffSvType<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>svType<span class="token punctuation">)</span><span class="token punctuation">;</span>
		server_map<span class="token punctuation">[</span>ID<span class="token punctuation">]</span><span class="token operator">=</span>svResources<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		svRunVmsNumber_map<span class="token punctuation">[</span>ID<span class="token punctuation">]</span><span class="token operator">=</span>svRunVmsNumber<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		sv_run_vms_map<span class="token punctuation">[</span>ID<span class="token punctuation">]</span><span class="token operator">=</span>svRunVms<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		svId_map<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>ID<span class="token operator">++</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span> j<span class="token operator">&lt;</span>svResources<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>svResources<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>type<span class="token operator">==</span>svType<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				server_map<span class="token punctuation">[</span>ID<span class="token punctuation">]</span><span class="token operator">=</span>svResources<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
				svRunVmsNumber_map<span class="token punctuation">[</span>ID<span class="token punctuation">]</span><span class="token operator">=</span>svRunVmsNumber<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
				sv_run_vms_map<span class="token punctuation">[</span>ID<span class="token punctuation">]</span><span class="token operator">=</span>svRunVms<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
				svId_map<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>ID<span class="token operator">++</span><span class="token punctuation">;</span>
				number<span class="token operator">++</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
		day_buy_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token string">"("</span> <span class="token operator">+</span> svType <span class="token operator">+</span> <span class="token string">", "</span> <span class="token operator">+</span> <span class="token function">to_string</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">")\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token comment">//存一下每日的购买信息</span>
	day_buy_info<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"(purchase, "</span> <span class="token operator">+</span> <span class="token function">to_string</span><span class="token punctuation">(</span>diffSvType<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">")\n"</span><span class="token punctuation">;</span><span class="token comment">//将购买种类存到数组的首位</span>
	purchase_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>day_buy_info<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//存一下每日的购买请求</span>

	<span class="token comment">//根据映射，为svResources和svRunVmsNumber和svRunVms中新增的部分重新赋值。是不是感觉很麻烦，是的</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>old_sv_number<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svResources<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		svResources<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>server_map<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		svRunVmsNumber<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>svRunVmsNumber_map<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		svRunVms<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>sv_run_vms_map<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>

	<span class="token comment">//使用 svId_map 映射去修改部署信息。并更新到全局部署信息中</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> <span class="token operator">&amp;</span>pair <span class="token operator">:</span> temp_dep_infos<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		string vmId <span class="token operator">=</span> pair<span class="token punctuation">.</span>first<span class="token punctuation">;</span>
		DeployInfo depInfo <span class="token operator">=</span> pair<span class="token punctuation">.</span>second<span class="token punctuation">;</span>
		depInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> svId_map<span class="token punctuation">[</span>depInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//修改部署到的服务器 Id</span>
		vmDeployInfos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> depInfo<span class="token punctuation">;</span><span class="token comment">//更新到全局变量</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">/*********************************************************/</span>

<span class="token comment">/*************************** 复杂的迁移策略 ***************************************/</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">findBestServerForMigrate</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>svIds<span class="token punctuation">,</span> DeployInfo <span class="token keyword">const</span> <span class="token operator">&amp;</span>vmDepInfo<span class="token punctuation">,</span> <span class="token keyword">int</span> start<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//迁移时，虚拟机应该放在哪台服务器上比较好。返回所在svIds中的索引，找不到就返回-1。并且可以指定从哪个位置开始往后找</span>
	<span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> node <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> minRest <span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">8</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> isDual <span class="token operator">=</span> vmDepInfo<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">3</span> <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> needCpu<span class="token operator">=</span>vmDepInfo<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> needMem<span class="token operator">=</span>vmDepInfo<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> rest <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是双节点部署的</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>start<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svIds<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>svIds<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">==</span>vmDepInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//双部署虚拟机不能迁移到本身所在的服务器上</span>
			Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>svIds<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>rest <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token operator">*</span><span class="token punctuation">(</span>needCpu<span class="token operator">+</span>needMem<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>rest <span class="token operator">-</span> <span class="token number">2</span><span class="token operator">*</span><span class="token punctuation">(</span>needCpu<span class="token operator">+</span>needMem<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//放入后的剩余资源量 cpu + mem</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest<span class="token operator">&lt;</span>minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是单节点部署的</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>start<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svIds<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>svIds<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token comment">//if(svIds[i]==vmDepInfo[0] &amp;&amp; vmDepInfo[3]==0) continue;//单部署虚拟机不能迁移到本身所在的服务器的相同节点上</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>sv<span class="token punctuation">.</span>memA <span class="token operator">-</span> needCpu <span class="token operator">-</span> needMem<span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest <span class="token operator">&lt;</span> minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//保证部署后的剩余资源量最小</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					node <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token comment">//if(svIds[i]==vmDepInfo[0] &amp;&amp; vmDepInfo[3]==1) continue;//单部署虚拟机不能迁移到本身所在的服务器的相同节点上</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>sv<span class="token punctuation">.</span>memB <span class="token operator">-</span> needCpu <span class="token operator">-</span> needMem<span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest <span class="token operator">&lt;</span> minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//保证部署后的剩余资源量最小</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					node <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span> <span class="token keyword">return</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">&#123;</span>index<span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span class="token comment">//返回可迁移到的服务器索引。不可能返回原服务器。</span>
	<span class="token keyword">else</span> <span class="token keyword">return</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">&#123;</span>index<span class="token punctuation">,</span> node<span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span class="token comment">//返回可迁移到的服务器索引，以及迁移到的节点。不可能返回原服务器和相同节点</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">migrate</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	vector<span class="token operator">&lt;</span>string<span class="token operator">></span> day_migrate_info<span class="token punctuation">&#123;</span><span class="token string">""</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span class="token comment">//用来存每日的迁移信息。先加入一个空字符串留给 migrate_num</span>
	<span class="token keyword">int</span> max_migrate_num <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">*</span>vmDeployInfos<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">100</span><span class="token punctuation">;</span><span class="token comment">//当日最大可迁移数量，不超过 max_migrate_num</span>
	<span class="token keyword">int</span> migrate_num <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//当日已迁移数量</span>

	vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> svIds<span class="token punctuation">;</span><span class="token comment">//收集服务器的Id，并计算每台服务器上的剩余资源量</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svResources<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		Server <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		sv<span class="token punctuation">.</span>rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA <span class="token operator">+</span> sv<span class="token punctuation">.</span>cpuB <span class="token operator">+</span> sv<span class="token punctuation">.</span>memA <span class="token operator">+</span> sv<span class="token punctuation">.</span>memB<span class="token punctuation">;</span><span class="token comment">//计算服务器上的剩余资源量	</span>
		svIds<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>

	<span class="token comment">//按服务器上的剩余资源量排序，第二序为其上运行的虚拟机个数。前提是服务器的rest属性值已被更新过，不然会出错</span>
	<span class="token function">sort</span><span class="token punctuation">(</span>svIds<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> svIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">&amp;</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>id1<span class="token punctuation">,</span> <span class="token keyword">int</span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>id2<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv1 <span class="token operator">=</span> svResources<span class="token punctuation">[</span>id1<span class="token punctuation">]</span><span class="token punctuation">;</span>
		Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv2 <span class="token operator">=</span> svResources<span class="token punctuation">[</span>id2<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>sv1<span class="token punctuation">.</span>rest <span class="token operator">==</span> sv2<span class="token punctuation">.</span>rest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">return</span> svRunVmsNumber<span class="token punctuation">[</span>id1<span class="token punctuation">]</span> <span class="token operator">&lt;</span> svRunVmsNumber<span class="token punctuation">[</span>id2<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">return</span> sv1<span class="token punctuation">.</span>rest <span class="token operator">></span> sv2<span class="token punctuation">.</span>rest<span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> canMigSvIds<span class="token punctuation">;</span><span class="token comment">//能够转移到的服务器</span>
	set<span class="token operator">&lt;</span>string<span class="token operator">></span> migratedVmIds<span class="token punctuation">;</span><span class="token comment">//已迁移过的虚拟机</span>
	set<span class="token operator">&lt;</span>string<span class="token operator">></span> delVmIds<span class="token punctuation">;</span><span class="token comment">//当天即将删除的虚拟机</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span>Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req <span class="token operator">:</span> daysReqs<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">if</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>type<span class="token operator">==</span><span class="token string">"del"</span><span class="token punctuation">)</span> delVmIds<span class="token punctuation">.</span><span class="token function">emplace</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">//迁移时会动态更新服务器上的剩余资源量，即rest属性的值</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svIds<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//从前往后遍历</span>
		<span class="token keyword">int</span> svId <span class="token operator">=</span> svIds<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		Server <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>rest<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuCore<span class="token operator">+</span>sv<span class="token punctuation">.</span>memSize<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0.018</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//如果这台服务器上的资源很满，那其上的虚拟机就不考虑迁移了</span>
		vector<span class="token operator">&lt;</span>string<span class="token operator">></span> vmIds <span class="token operator">=</span> svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//服务器上的虚拟机们</span>

		<span class="token comment">//按已部署虚拟机占用的资源量排序</span>
		<span class="token function">sort</span><span class="token punctuation">(</span>vmIds<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> vmIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">&amp;</span><span class="token punctuation">]</span><span class="token punctuation">(</span>string <span class="token keyword">const</span> <span class="token operator">&amp;</span>vm1<span class="token punctuation">,</span> string <span class="token keyword">const</span> <span class="token operator">&amp;</span>vm2<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			DeployInfo <span class="token keyword">const</span> <span class="token operator">&amp;</span>dep1 <span class="token operator">=</span> vmDeployInfos<span class="token punctuation">[</span>vm1<span class="token punctuation">]</span><span class="token punctuation">;</span>
			DeployInfo <span class="token keyword">const</span> <span class="token operator">&amp;</span>dep2 <span class="token operator">=</span> vmDeployInfos<span class="token punctuation">[</span>vm2<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>dep1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">+</span>dep1<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">==</span> dep2<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">+</span>dep2<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token keyword">return</span> dep1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">></span> dep2<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token keyword">return</span> dep1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">+</span>dep1<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">></span> dep2<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">+</span>dep2<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token keyword">for</span><span class="token punctuation">(</span>string <span class="token keyword">const</span> <span class="token operator">&amp;</span>vmId <span class="token operator">:</span> vmIds<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//遍历每台虚拟机</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>migrate_num <span class="token operator">==</span> max_migrate_num<span class="token punctuation">)</span> <span class="token keyword">goto</span> STOP_MIGRATE<span class="token punctuation">;</span><span class="token comment">//达到最大迁移次数</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>delVmIds<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token operator">!=</span>delVmIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment">//这台虚拟机即将被删除，就不再迁移了</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>migratedVmIds<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token operator">!=</span>migratedVmIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment">//这台虚拟机之前迁移过，就不再迁移了</span>
			DeployInfo <span class="token operator">&amp;</span>deInfo <span class="token operator">=</span> vmDeployInfos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//虚拟机部署信息</span>
			<span class="token keyword">int</span> dual <span class="token operator">=</span> deInfo<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">3</span> <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> occupiedCpu<span class="token operator">=</span>deInfo<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> occupiedMem<span class="token operator">=</span>deInfo<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

			vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> ans <span class="token operator">=</span> <span class="token function">findBestServerForMigrate</span><span class="token punctuation">(</span>svIds<span class="token punctuation">,</span> deInfo<span class="token punctuation">,</span> i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//找到最合适迁移到的服务器</span>
			<span class="token keyword">int</span> index<span class="token operator">=</span>ans<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> goal_svId<span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>index <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span><span class="token comment">//表示找不到能放得下的可以迁移到的服务器</span>
				ans <span class="token operator">=</span> <span class="token function">findBestServerForMigrate</span><span class="token punctuation">(</span>canMigSvIds<span class="token punctuation">,</span> deInfo<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//继续在canMigSvIds里面找</span>
				index <span class="token operator">=</span> ans<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>index <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">||</span> canMigSvIds<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token operator">==</span>svId<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//暂时考虑完全避免往自己上面迁移</span>
					<span class="token keyword">if</span> <span class="token punctuation">(</span>canMigSvIds<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">0</span> <span class="token operator">||</span> canMigSvIds<span class="token punctuation">[</span>canMigSvIds<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">!=</span>svId<span class="token punctuation">)</span> canMigSvIds<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>svId<span class="token punctuation">)</span><span class="token punctuation">;</span>
					<span class="token keyword">continue</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					goal_svId <span class="token operator">=</span> canMigSvIds<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//目标服务器Id</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
				goal_svId <span class="token operator">=</span> svIds<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//目标服务器Id</span>
			<span class="token punctuation">&#125;</span>
			migratedVmIds<span class="token punctuation">.</span><span class="token function">emplace</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//记录该虚拟机已迁移过</span>
			Server <span class="token operator">&amp;</span>goal_sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>goal_svId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//目标服务器，一旦找到就表示肯定能放下</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>dual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//双节点部署</span>
				sv<span class="token punctuation">.</span>cpuA <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span><span class="token comment">//原服务器资源增加</span>
				sv<span class="token punctuation">.</span>cpuB <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memA <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memB <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>sv<span class="token punctuation">.</span>memB<span class="token punctuation">;</span><span class="token comment">//更新剩余资源量</span>
				vector<span class="token operator">&lt;</span>string<span class="token operator">></span> <span class="token operator">&amp;</span>VMIDS<span class="token operator">=</span>svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span>
				VMIDS<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">remove</span><span class="token punctuation">(</span>VMIDS<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> vmId<span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//没错删除就是这么复杂</span>
				goal_sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> occupiedCpu<span class="token punctuation">;</span><span class="token comment">//目标服务器资源减少</span>
				goal_sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> occupiedCpu<span class="token punctuation">;</span>
				goal_sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> occupiedMem<span class="token punctuation">;</span>
				goal_sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> occupiedMem<span class="token punctuation">;</span>
				goal_sv<span class="token punctuation">.</span>rest <span class="token operator">=</span> goal_sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>memB<span class="token punctuation">;</span><span class="token comment">//更新剩余资源量</span>
				svRunVmsNumber<span class="token punctuation">[</span>goal_svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
				svRunVms<span class="token punctuation">[</span>goal_svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token comment">//修改部署信息</span>
				deInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> goal_svId<span class="token punctuation">;</span>
				migrate_num<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//当日已迁移数量 + 1</span>
				day_migrate_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token string">"("</span> <span class="token operator">+</span> vmId <span class="token operator">+</span> <span class="token string">", "</span> <span class="token operator">+</span> <span class="token function">to_string</span><span class="token punctuation">(</span>goal_svId<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">")\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//单节点部署</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>deInfo<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//原来部署在A节点还是B节点</span>
					sv<span class="token punctuation">.</span>cpuA <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memA <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					sv<span class="token punctuation">.</span>cpuB <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memB <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
				svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>sv<span class="token punctuation">.</span>memB<span class="token punctuation">;</span><span class="token comment">//更新剩余资源量</span>
				vector<span class="token operator">&lt;</span>string<span class="token operator">></span> <span class="token operator">&amp;</span>VMIDS<span class="token operator">=</span>svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span>
				VMIDS<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">remove</span><span class="token punctuation">(</span>VMIDS<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> vmId<span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//没错删除就是这么复杂</span>
				<span class="token keyword">int</span> node<span class="token operator">=</span>ans<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//现在可迁移到的节点</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>node<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
					goal_sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> occupiedCpu<span class="token punctuation">;</span>
					goal_sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> occupiedMem<span class="token punctuation">;</span>
					svRunVmsNumber<span class="token punctuation">[</span>goal_svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
					svRunVms<span class="token punctuation">[</span>goal_svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
					goal_sv<span class="token punctuation">.</span>rest <span class="token operator">=</span> goal_sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>memB<span class="token punctuation">;</span><span class="token comment">//更新剩余资源量</span>
					<span class="token comment">//修改部署信息</span>
					deInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> goal_svId<span class="token punctuation">;</span>
					deInfo<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
					migrate_num<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//当日已迁移数量 + 1</span>
					day_migrate_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token string">"("</span> <span class="token operator">+</span> vmId <span class="token operator">+</span> <span class="token string">", "</span> <span class="token operator">+</span> <span class="token function">to_string</span><span class="token punctuation">(</span>goal_svId<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">", A)\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					goal_sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> occupiedCpu<span class="token punctuation">;</span>
					goal_sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> occupiedMem<span class="token punctuation">;</span>
					svRunVmsNumber<span class="token punctuation">[</span>goal_svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
					svRunVms<span class="token punctuation">[</span>goal_svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
					goal_sv<span class="token punctuation">.</span>rest <span class="token operator">=</span> goal_sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>goal_sv<span class="token punctuation">.</span>memB<span class="token punctuation">;</span><span class="token comment">//更新剩余资源量</span>
					<span class="token comment">//修改部署信息</span>
					deInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> goal_svId<span class="token punctuation">;</span>
					deInfo<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
					migrate_num<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//当日已迁移数量 + 1</span>
					day_migrate_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token string">"("</span> <span class="token operator">+</span> vmId <span class="token operator">+</span> <span class="token string">", "</span> <span class="token operator">+</span> <span class="token function">to_string</span><span class="token punctuation">(</span>goal_svId<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">", B)\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>

STOP_MIGRATE<span class="token operator">:</span>
<span class="token comment">//	if(day==0 || day%10==0)&#123;</span>
<span class="token comment">//		cout&lt;&lt;day&lt;&lt;" --> "&lt;&lt;max_migrate_num&lt;&lt;" --> "&lt;&lt;migrate_num&lt;&lt;endl;</span>
<span class="token comment">//	&#125;</span>
	day_migrate_info<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"(migration, "</span> <span class="token operator">+</span> <span class="token function">to_string</span><span class="token punctuation">(</span>migrate_num<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">")\n"</span><span class="token punctuation">;</span><span class="token comment">//将migrate_num转化成字符串放到数组的首位</span>
	MIGRATE_NUMBER <span class="token operator">+=</span> migrate_num<span class="token punctuation">;</span>
	migrate_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>day_migrate_info<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//存一下每日的迁移信息</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">/*********************************************************************************/</span>

<span class="token comment">/********************* 虚拟机请求资源（vmCpu + vmMem）排序 *********************/</span>
<span class="token keyword">void</span> <span class="token function">quickSort_cpumem_down</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>Request<span class="token operator">></span> <span class="token operator">&amp;</span>reqs<span class="token punctuation">,</span> <span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span>l <span class="token operator">&lt;</span> r<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">int</span> i<span class="token operator">=</span>l<span class="token punctuation">,</span> j<span class="token operator">=</span>r<span class="token punctuation">;</span>
		Request req<span class="token operator">=</span>reqs<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>i <span class="token operator">&lt;</span> j<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			<span class="token keyword">while</span><span class="token punctuation">(</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>vmCpu<span class="token operator">+</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>vmMem <span class="token operator">&lt;</span> req<span class="token punctuation">.</span>vmCpu<span class="token operator">+</span>req<span class="token punctuation">.</span>vmMem <span class="token operator">&amp;&amp;</span> i<span class="token operator">&lt;</span>j<span class="token punctuation">)</span> j<span class="token operator">--</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">&lt;</span> j<span class="token punctuation">)</span> reqs<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">while</span><span class="token punctuation">(</span>reqs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>vmCpu<span class="token operator">+</span>reqs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>vmMem <span class="token operator">>=</span> req<span class="token punctuation">.</span>vmCpu<span class="token operator">+</span>req<span class="token punctuation">.</span>vmMem <span class="token operator">&amp;&amp;</span> i<span class="token operator">&lt;</span>j<span class="token punctuation">)</span> i<span class="token operator">++</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">&lt;</span> j<span class="token punctuation">)</span> reqs<span class="token punctuation">[</span>j<span class="token operator">--</span><span class="token punctuation">]</span> <span class="token operator">=</span> reqs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		reqs<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> req<span class="token punctuation">;</span>
		<span class="token function">quickSort_cpumem_down</span><span class="token punctuation">(</span>reqs<span class="token punctuation">,</span> l<span class="token punctuation">,</span> i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">quickSort_cpumem_down</span><span class="token punctuation">(</span>reqs<span class="token punctuation">,</span> i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token keyword">void</span> <span class="token function">segmentSort</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>Request<span class="token operator">></span> <span class="token operator">&amp;</span>reqs<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//以删除请求分段排序，为的是不打乱删除的顺序</span>
	<span class="token keyword">int</span> start<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>reqs<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>reqs<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>type<span class="token operator">==</span><span class="token string">"del"</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			vector<span class="token operator">&lt;</span>Request<span class="token operator">></span> speReqs<span class="token punctuation">,</span>dualReqs<span class="token punctuation">,</span>sgleReqs<span class="token punctuation">;</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>start<span class="token punctuation">;</span> j<span class="token operator">&lt;</span>i<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token keyword">float</span> ratio <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>vmCpu<span class="token operator">/</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>ratio<span class="token operator">&lt;=</span><span class="token number">0.28</span> <span class="token operator">||</span> ratio<span class="token operator">>=</span><span class="token number">6.8</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
					speReqs<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					<span class="token keyword">continue</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
<span class="token comment">//				if(ratio&lt;0.28 || ratio>6.8)&#123;</span>
<span class="token comment">//					speReqs.emplace_back(reqs[j]);</span>
<span class="token comment">//					continue;</span>
<span class="token comment">//				&#125;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>isDual<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span> dualReqs<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">else</span> sgleReqs<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>reqs<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token function">quickSort_cpumem_down</span><span class="token punctuation">(</span>speReqs<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> speReqs<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">quickSort_cpumem_down</span><span class="token punctuation">(</span>dualReqs<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> dualReqs<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">quickSort_cpumem_down</span><span class="token punctuation">(</span>sgleReqs<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> sgleReqs<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span>Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token operator">:</span>sgleReqs<span class="token punctuation">)</span> reqs<span class="token punctuation">[</span>start<span class="token operator">++</span><span class="token punctuation">]</span><span class="token operator">=</span>req<span class="token punctuation">;</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span>Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token operator">:</span>dualReqs<span class="token punctuation">)</span> reqs<span class="token punctuation">[</span>start<span class="token operator">++</span><span class="token punctuation">]</span><span class="token operator">=</span>req<span class="token punctuation">;</span>
			<span class="token keyword">for</span><span class="token punctuation">(</span>Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token operator">:</span>speReqs<span class="token punctuation">)</span> reqs<span class="token punctuation">[</span>start<span class="token operator">++</span><span class="token punctuation">]</span><span class="token operator">=</span>req<span class="token punctuation">;</span>
			<span class="token comment">//quickSort_cpumem_down(reqs, start, i-1);</span>
			start<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token comment">//之前就是忘了这一步</span>
	<span class="token function">quickSort_cpumem_down</span><span class="token punctuation">(</span>reqs<span class="token punctuation">,</span> start<span class="token punctuation">,</span> reqs<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">/*********************************************************************************/</span>

<span class="token comment">//直接部署时。</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">findBestServer</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token keyword">const</span> <span class="token operator">&amp;</span>svIds<span class="token punctuation">,</span> Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token punctuation">,</span> <span class="token keyword">int</span> start<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> node <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> needCpu <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmCpu<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmCpu<span class="token punctuation">;</span>
	<span class="token keyword">int</span>	needMem <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmMem<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span>
	<span class="token keyword">int</span> minRest <span class="token operator">=</span> <span class="token operator">~</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">8</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> rest <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//双节点</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>start<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svIds<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>svIds<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token keyword">float</span> ratio1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">-</span>needCpu<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>memA<span class="token operator">-</span>needMem<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">float</span> ratio2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">-</span>needCpu<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>memB<span class="token operator">-</span>needMem<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>ratio1<span class="token operator">&lt;</span><span class="token number">0.13</span> <span class="token operator">||</span> ratio1<span class="token operator">></span><span class="token number">7.5</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//保证A节点不失衡</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>ratio2<span class="token operator">&lt;</span><span class="token number">0.13</span> <span class="token operator">||</span> ratio2<span class="token operator">></span><span class="token number">7.5</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//保证B节点不失衡</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA<span class="token operator">+</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">+</span>sv<span class="token punctuation">.</span>memA<span class="token operator">+</span>sv<span class="token punctuation">.</span>memB <span class="token operator">-</span> req<span class="token punctuation">.</span>vmCpu <span class="token operator">-</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest <span class="token operator">&lt;</span> minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//保证部署后的剩余资源量最小</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//单节点</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span>start<span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svIds<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
			Server <span class="token keyword">const</span> <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>svIds<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memA<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token keyword">float</span> ratio1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuA<span class="token operator">-</span>needCpu<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>memA<span class="token operator">-</span>needMem<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>ratio1<span class="token operator">&lt;</span><span class="token number">0.13</span> <span class="token operator">||</span> ratio1<span class="token operator">></span><span class="token number">7.5</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//保证在A节点上部署后不失衡</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuA <span class="token operator">-</span> req<span class="token punctuation">.</span>vmCpu<span class="token punctuation">;</span><span class="token comment">//只考虑cpu的剩余量效果竟然要好一些</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest <span class="token operator">&lt;</span> minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//保证部署后的剩余资源量最小</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					node <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">>=</span>needCpu <span class="token operator">&amp;&amp;</span> sv<span class="token punctuation">.</span>memB<span class="token operator">>=</span>needMem<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				<span class="token keyword">float</span> ratio2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">float</span><span class="token punctuation">)</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>cpuB<span class="token operator">-</span>needCpu<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>sv<span class="token punctuation">.</span>memB<span class="token operator">-</span>needMem<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>ratio2<span class="token operator">&lt;</span><span class="token number">0.13</span> <span class="token operator">||</span> ratio2<span class="token operator">></span><span class="token number">7.5</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token comment">//保证在B节点上部署后不失衡</span>
				rest <span class="token operator">=</span> sv<span class="token punctuation">.</span>cpuB <span class="token operator">-</span> req<span class="token punctuation">.</span>vmCpu<span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>rest <span class="token operator">&lt;</span> minRest<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//保证部署后的剩余资源量最小</span>
					index <span class="token operator">=</span> i<span class="token punctuation">;</span>
					node <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
					minRest<span class="token operator">=</span>rest<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>isDual<span class="token punctuation">)</span> <span class="token keyword">return</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">&#123;</span>index<span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span class="token comment">//返回找到的服务器索引</span>
	<span class="token keyword">else</span> <span class="token keyword">return</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">&#123;</span>index<span class="token punctuation">,</span> node<span class="token punctuation">&#125;</span><span class="token punctuation">;</span><span class="token comment">//返回找到的服务器索引，以及部署节点</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//处理每日请求</span>
<span class="token keyword">void</span> <span class="token function">handleOneDayRequests</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	vector<span class="token operator">&lt;</span>Request<span class="token operator">></span> sortedReqs <span class="token operator">=</span> daysReqs<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//复制一份数据，保持原数据不变</span>
	<span class="token function">segmentSort</span><span class="token punctuation">(</span>sortedReqs<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//分段排序</span>

	<span class="token function">migrate</span><span class="token punctuation">(</span>day<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//迁移</span>

	<span class="token comment">//分下类</span>
	vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> emptySvIds<span class="token punctuation">;</span><span class="token comment">//空服务器Id</span>
	vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> notEmptySvIds<span class="token punctuation">;</span><span class="token comment">//非空服务器Id</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svResources<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>svRunVmsNumber<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> notEmptySvIds<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">else</span> emptySvIds<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>

	<span class="token comment">//能放的就放，放不下的就记录下来，稍后购买。（该记录中可能含有del，主要是可能有该天添加，该天删除的虚拟机）</span>
	vector<span class="token operator">&lt;</span>Request<span class="token operator">></span> cantHoldReqs<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span>Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req<span class="token operator">:</span>sortedReqs<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		string vmId<span class="token operator">=</span>req<span class="token punctuation">.</span>vmId<span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>type<span class="token operator">==</span><span class="token string">"add"</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//添加请求</span>
			vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">></span> ans <span class="token operator">=</span> <span class="token function">findBestServer</span><span class="token punctuation">(</span>notEmptySvIds<span class="token punctuation">,</span> req<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//先在非空服务器上查找</span>
			<span class="token keyword">int</span> svId<span class="token punctuation">,</span> index<span class="token operator">=</span>ans<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>index <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				ans <span class="token operator">=</span> <span class="token function">findBestServer</span><span class="token punctuation">(</span>emptySvIds<span class="token punctuation">,</span> req<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//再在空服务器上查找</span>
				index <span class="token operator">=</span> ans<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>index <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>cantHoldReqs<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>req<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">continue</span><span class="token punctuation">;</span><span class="token punctuation">&#125;</span><span class="token comment">//实在是放不下时，记录下该请求</span>
				svId <span class="token operator">=</span> emptySvIds<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//服务器Id</span>
				<span class="token comment">//将该服务器Id从空移到非空</span>
				emptySvIds<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">remove</span><span class="token punctuation">(</span>emptySvIds<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> emptySvIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> svId<span class="token punctuation">)</span><span class="token punctuation">,</span> emptySvIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				notEmptySvIds<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>svId<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
				svId <span class="token operator">=</span> notEmptySvIds<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//服务器Id</span>
			<span class="token punctuation">&#125;</span>
			<span class="token keyword">int</span> isDual <span class="token operator">=</span> req<span class="token punctuation">.</span>isDual<span class="token punctuation">;</span>
			<span class="token keyword">int</span> needCpu <span class="token operator">=</span> isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmCpu<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmCpu<span class="token punctuation">;</span>
			<span class="token keyword">int</span> needMem <span class="token operator">=</span> isDual <span class="token operator">?</span> req<span class="token punctuation">.</span>vmMem<span class="token operator">/</span><span class="token number">2</span> <span class="token operator">:</span> req<span class="token punctuation">.</span>vmMem<span class="token punctuation">;</span>
			Server <span class="token operator">&amp;</span>sv <span class="token operator">=</span> svResources<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//涉及到修改资源量了</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>isDual<span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//双节点部署</span>
				sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
				vmDeployInfos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
				svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
				svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//单节点部署</span>
				<span class="token keyword">int</span> node <span class="token operator">=</span> ans<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//在返回的结果中看看它该部署到哪个节点合适</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>node<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
					sv<span class="token punctuation">.</span>cpuA <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memA <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
					vmDeployInfos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
					svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
					svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					sv<span class="token punctuation">.</span>cpuB <span class="token operator">-=</span> needCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memB <span class="token operator">-=</span> needMem<span class="token punctuation">;</span>
					vmDeployInfos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span> <span class="token operator">=</span> DeployInfo<span class="token punctuation">&#123;</span>svId<span class="token punctuation">,</span> needCpu<span class="token punctuation">,</span> needMem<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
					svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
					svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//删除请求</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>vmDeployInfos<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>vmId<span class="token punctuation">)</span> <span class="token operator">==</span> vmDeployInfos<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//该虚拟机是本应该当天部署的，但是没部署上，被存起来了</span>
				cantHoldReqs<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>req<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//那么当天对该虚拟机的删除也需要存起来。保持相对处理位置不变</span>
				<span class="token keyword">continue</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
			DeployInfo vmDeInfo <span class="token operator">=</span> vmDeployInfos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> svId<span class="token operator">=</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> occupiedCpu<span class="token operator">=</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> occupiedMem<span class="token operator">=</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//获取部署时的关键信息</span>
			Server <span class="token operator">&amp;</span>sv<span class="token operator">=</span>svResources<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//涉及到修改资源量了</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>vmDeInfo<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是双节点部署的</span>
				sv<span class="token punctuation">.</span>cpuA <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>cpuB <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memA <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				sv<span class="token punctuation">.</span>memB <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是单节点部署的</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span>vmDeInfo<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//之前是部署在A节点上</span>
					sv<span class="token punctuation">.</span>cpuA <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memA <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
					sv<span class="token punctuation">.</span>cpuB <span class="token operator">+=</span> occupiedCpu<span class="token punctuation">;</span>
					sv<span class="token punctuation">.</span>memB <span class="token operator">+=</span> occupiedMem<span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
			<span class="token punctuation">&#125;</span>
			<span class="token comment">//vmDeployInfos.erase(vmId);//暂时不删，不然做编号映射时会出问题。最后统一删除部署信息</span>
			svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">--</span><span class="token punctuation">;</span>
			vector<span class="token operator">&lt;</span>string<span class="token operator">></span> <span class="token operator">&amp;</span>VMIDS<span class="token operator">=</span>svRunVms<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token punctuation">;</span>
			VMIDS<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">remove</span><span class="token punctuation">(</span>VMIDS<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> vmId<span class="token punctuation">)</span><span class="token punctuation">,</span> VMIDS<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//没错删除就是这么复杂</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>svRunVmsNumber<span class="token punctuation">[</span>svId<span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//如果该服务器为空了，将该服务器Id从非空移到空</span>
				notEmptySvIds<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token function">remove</span><span class="token punctuation">(</span>notEmptySvIds<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> notEmptySvIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> svId<span class="token punctuation">)</span><span class="token punctuation">,</span> notEmptySvIds<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				emptySvIds<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>svId<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>

	<span class="token comment">//考虑处理前面放不下的请求。下面的操作会改变 svResources 的大小，也就是会新买一批服务器紧接着放在后面</span>
	<span class="token function">buyServersForReqs</span><span class="token punctuation">(</span>day<span class="token punctuation">,</span> cantHoldReqs<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//买服务器，放请求，并做好映射，更新全局部署信息</span>

	<span class="token comment">//保存当日的部署信息</span>
	vector<span class="token operator">&lt;</span>string<span class="token operator">></span> day_deploy_info<span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span>Request <span class="token keyword">const</span> <span class="token operator">&amp;</span>req <span class="token operator">:</span> daysReqs<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//顺序重新遍历当日请求</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>type<span class="token operator">==</span><span class="token string">"add"</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//添加请求</span>
			string vmId<span class="token operator">=</span>req<span class="token punctuation">.</span>vmId<span class="token punctuation">;</span>
			DeployInfo depInfo <span class="token operator">=</span> vmDeployInfos<span class="token punctuation">[</span>vmId<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//得到部署信息</span>
			string svId <span class="token operator">=</span> <span class="token function">to_string</span><span class="token punctuation">(</span>depInfo<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>depInfo<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
				day_deploy_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token string">"("</span> <span class="token operator">+</span> svId <span class="token operator">+</span> <span class="token string">")\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
				string nodeInfo <span class="token operator">=</span> depInfo<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span> <span class="token operator">?</span> <span class="token string">"A"</span> <span class="token operator">:</span> <span class="token string">"B"</span><span class="token punctuation">;</span>
				day_deploy_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span><span class="token string">"("</span> <span class="token operator">+</span> svId <span class="token operator">+</span> <span class="token string">", "</span> <span class="token operator">+</span> nodeInfo <span class="token operator">+</span> <span class="token string">")\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">&#125;</span>
		<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span><span class="token comment">//删除请求没有任何输出</span>
			<span class="token comment">//但是在这里统一删除，需要删除的部署信息。我简直是太机智了。如果在前面就删掉了某台虚拟机的部署信息，那么在这里遍历时就可能找不到它的部署信息了，那我怎么输出日志嘛</span>
			vmDeployInfos<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>vmId<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
	deploy_info<span class="token punctuation">.</span><span class="token function">emplace_back</span><span class="token punctuation">(</span>day_deploy_info<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//存一下每日的部署请求</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//计算每日的耗电成本</span>
<span class="token keyword">void</span> <span class="token function">computePowerCost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>svRunVmsNumber<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>svRunVmsNumber<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            POWERCOST <span class="token operator">+=</span> svResources<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>powerCost<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//打印输出结果</span>
<span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">int</span> startDay<span class="token punctuation">,</span> <span class="token keyword">int</span> endDay<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token operator">=</span>startDay<span class="token punctuation">;</span> day<span class="token operator">&lt;</span>endDay<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span> day<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//遍历[startDay, endDay]</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span>string <span class="token operator">&amp;</span>info<span class="token operator">:</span>purchase_info<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//每天购买</span>
			cout<span class="token operator">&lt;&lt;</span>info<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span>string <span class="token operator">&amp;</span>info<span class="token operator">:</span>migrate_info<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//每天迁移</span>
			cout<span class="token operator">&lt;&lt;</span>info<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span>string <span class="token operator">&amp;</span>info<span class="token operator">:</span>deploy_info<span class="token punctuation">[</span>day<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//每天部署</span>
			cout<span class="token operator">&lt;&lt;</span>info<span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">void</span> <span class="token function">cloudResourceSchedulingAlgorithm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token function">readServerAndVmInfos</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//读取服务器和虚拟机信息</span>
	<span class="token keyword">int</span> dayReqNumber<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>reqDays<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//请求有多少天</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>windowDays<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//天数窗口，可以理解为每天处理请求时，只知道该窗口内的后续请求</span>

	<span class="token comment">//先读入 windowDays 天的请求</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> day<span class="token operator">&lt;</span>windowDays<span class="token punctuation">;</span> day<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>dayReqNumber<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">readDayRequests</span><span class="token punctuation">(</span>day<span class="token punctuation">,</span> dayReqNumber<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>

	<span class="token comment">//再一边处理现有请求，一边输出决策，一边读入后续请求数据。复赛要求这么做</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> day<span class="token operator">&lt;</span><span class="token punctuation">(</span>reqDays <span class="token operator">-</span> windowDays<span class="token punctuation">)</span><span class="token punctuation">;</span> day<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token comment">//处理现有请求</span>
		<span class="token function">handleOneDayRequests</span><span class="token punctuation">(</span>day<span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token function">computePowerCost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>day<span class="token operator">==</span><span class="token number">0</span> <span class="token operator">||</span> day<span class="token operator">%</span><span class="token number">50</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Finished requests of day : %d\n"</span><span class="token punctuation">,</span> day<span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token comment">//输出当天决策，刷新输出缓存区，相应赛题的要求</span>
		<span class="token comment">//print(day, day);</span>
		<span class="token comment">//fflush(stdout);</span>

		<span class="token comment">//读入后续请求</span>
		<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>dayReqNumber<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">readDayRequests</span><span class="token punctuation">(</span>windowDays<span class="token operator">+</span>day<span class="token punctuation">,</span> dayReqNumber<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>

	<span class="token comment">//处理剩余天的请求</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> day<span class="token operator">=</span>reqDays<span class="token operator">-</span>windowDays<span class="token punctuation">;</span> day<span class="token operator">&lt;</span>reqDays<span class="token punctuation">;</span> day<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token function">handleOneDayRequests</span><span class="token punctuation">(</span>day<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">computePowerCost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>day<span class="token operator">==</span><span class="token number">0</span> <span class="token operator">||</span> day<span class="token operator">%</span><span class="token number">50</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Finished requests of day : %d\n"</span><span class="token punctuation">,</span> day<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token comment">//输出剩余天的决策</span>
	<span class="token comment">//print(reqDays-windowDays, reqDays-1);</span>

<span class="token punctuation">&#125;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
<span class="token comment">//	freopen(OUTPUT_REDIRECTION, "w", stdout);</span>
	clock_t start<span class="token punctuation">,</span> finish<span class="token punctuation">;</span>

	<span class="token comment">//第一份文件</span>
	start <span class="token operator">=</span> <span class="token function">clock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">freopen</span><span class="token punctuation">(</span>INPUT_REDIRECTION_1<span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">cloudResourceSchedulingAlgorithm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	finish <span class="token operator">=</span> <span class="token function">clock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	TOTALCOST <span class="token operator">=</span> SERVERCOST <span class="token operator">+</span> POWERCOST<span class="token punctuation">;</span><span class="token comment">//总成本</span>
	SC <span class="token operator">+=</span> SERVERCOST<span class="token punctuation">,</span> PC <span class="token operator">+=</span> POWERCOST<span class="token punctuation">,</span> TC <span class="token operator">+=</span> TOTALCOST<span class="token punctuation">;</span>
	TOTAL_MIGRATE <span class="token operator">+=</span> MIGRATE_NUMBER<span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\nCompute Time: %f s \n"</span><span class="token punctuation">,</span> <span class="token keyword">double</span><span class="token punctuation">(</span>finish<span class="token operator">-</span>start<span class="token punctuation">)</span><span class="token operator">/</span>CLOCKS_PER_SEC<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Server Cost: %lld \nPower Cost: %lld \nTotal Cost: %lld \n"</span><span class="token punctuation">,</span> SERVERCOST<span class="token punctuation">,</span> POWERCOST<span class="token punctuation">,</span> TOTALCOST<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Migrate number: %d\n\n"</span><span class="token punctuation">,</span> MIGRATE_NUMBER<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">//变量清零</span>
	svInfos<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	vmInfos<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	svCostServers<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	pwCostServers<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	daysReqs<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	svResources<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	vmDeployInfos<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	svRunVmsNumber<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	svRunVms<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	purchase_info<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	migrate_info<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	deploy_info<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	SERVERCOST<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	POWERCOST<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	TOTALCOST<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	MIGRATE_NUMBER<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>

	<span class="token comment">//第二份文件</span>
	start <span class="token operator">=</span> <span class="token function">clock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">freopen</span><span class="token punctuation">(</span>INPUT_REDIRECTION_2<span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">,</span> <span class="token constant">stdin</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">cloudResourceSchedulingAlgorithm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	finish <span class="token operator">=</span> <span class="token function">clock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	TOTALCOST <span class="token operator">=</span> SERVERCOST <span class="token operator">+</span> POWERCOST<span class="token punctuation">;</span><span class="token comment">//总成本</span>
	SC <span class="token operator">+=</span> SERVERCOST<span class="token punctuation">,</span> PC <span class="token operator">+=</span> POWERCOST<span class="token punctuation">,</span> TC <span class="token operator">+=</span> TOTALCOST<span class="token punctuation">;</span>
	TOTAL_MIGRATE <span class="token operator">+=</span> MIGRATE_NUMBER<span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\nCompute Time: %f s \n"</span><span class="token punctuation">,</span> <span class="token keyword">double</span><span class="token punctuation">(</span>finish<span class="token operator">-</span>start<span class="token punctuation">)</span><span class="token operator">/</span>CLOCKS_PER_SEC<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Server Cost: %lld \nPower Cost: %lld \nTotal Cost: %lld \n"</span><span class="token punctuation">,</span> SERVERCOST<span class="token punctuation">,</span> POWERCOST<span class="token punctuation">,</span> TOTALCOST<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Migrate number: %d\n"</span><span class="token punctuation">,</span> MIGRATE_NUMBER<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">//打印两份文件的总成本</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\nServer Cost: %lld \nPower Cost: %lld \nTotal Cost: %lld \n"</span><span class="token punctuation">,</span> SC<span class="token punctuation">,</span> PC<span class="token punctuation">,</span> TC<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Migrate number: %d \n\n"</span><span class="token punctuation">,</span> TOTAL_MIGRATE<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span></code></pre>
                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">YangChongZhi</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://yczlab.top/posts/338d62e1.html">http://yczlab.top/posts/338d62e1.html</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">YangChongZhi</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/%E6%AF%94%E8%B5%9B/">
                                    <span class="chip bg-color">比赛</span>
                                </a>
                            
                                <a href="/tags/C/">
                                    <span class="chip bg-color">C++</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments textarea {
        box-sizing: border-box;
        background: url("/medias/comment_bg.webp") 100% 100% no-repeat;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #4cbf30;
        font-weight: 500;
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="vcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'jJDtz1MXtl9cRxLcrXYvlvhd-9Nh9j0Va',
        appKey: 'UpF6BuVYYhInv6M8vXS0dWRt',
        pageSize: '10',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'false' === 'true',
        avatar: 'monsterid',
		recordIP: 'true',
		enableQQ: 'true',
		requiredFields: ["nick", "mail"],
        lang: 'zh-cn',
        placeholder: '请开始你的表演吧！\n注意博客留言要求填写昵称、邮箱'
    });
</script>

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/posts/827ca9cd.html">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/14.jpg" class="responsive-img" alt="十大经典排序算法">
                        
                        <span class="card-title">十大经典排序算法</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                              
引言：
排序算法是《数据结构与算法》中最基本的算法之一。这里对十大经典的排序算法做一下解释说明。不知道的道友可以来这里扫下盲。转自“菜鸟教程”。

说明  排序算法可以分为内部排序和外部排序，内部排序是数据记录在内存中进行排序，而外部
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-05-02
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Algorithm/" class="post-category">
                                    Algorithm
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Java/">
                        <span class="chip bg-color">Java</span>
                    </a>
                    
                    <a href="/tags/Algorithm/">
                        <span class="chip bg-color">Algorithm</span>
                    </a>
                    
                    <a href="/tags/DataStructure/">
                        <span class="chip bg-color">DataStructure</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/posts/f15db2c6.html">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/11.jpg" class="responsive-img" alt="最长递增子序列">
                        
                        <span class="card-title">最长递增子序列</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                              
引言：
最长递增子序列问题是一个很基本、较常见的小问题，但这个问题的求解方法却并不那么显而易见，需要较深入的思考和较好的算法素养才能得出良好的算法。这个问题能运用学过的基本的算法分析和设计的方法与思想，能够锻炼设计较复杂算法的思维。转
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-03-04
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Algorithm/" class="post-category">
                                    Algorithm
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Java/">
                        <span class="chip bg-color">Java</span>
                    </a>
                    
                    <a href="/tags/Algorithm/">
                        <span class="chip bg-color">Algorithm</span>
                    </a>
                    
                    <a href="/tags/DataStructure/">
                        <span class="chip bg-color">DataStructure</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="7565528433"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.4'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2024</span>
            
            <span id="year">2019</span>
            <a href="/about" target="_blank">YangChongZhi</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">236.5k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2019";
                    var startMonth = "6";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
            <span id="icp"><img src="/medias/icp.png" style="vertical-align: text-bottom;" />
                <a href="http://beian.miit.gov.cn/" target="_blank">渝ICP备2021001798号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/yczlab" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="https://gitee.com/yczlab" class="tooltipped" target="_blank" data-tooltip="访问我的码云" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2685733832@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2685733832" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2685733832" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2685733832" class="tooltipped" target="_blank" data-tooltip="微信联系我: yczlab" data-position="top" data-delay="50">
        <i class="fab fa-weixin"></i>
    </a>



    <a href="https://weibo.com/yczlab" class="tooltipped" target="_blank" data-tooltip="关注我的微博: https://weibo.com/yczlab" data-position="top" data-delay="50">
        <i class="fab fa-weibo"></i>
    </a>



    <a href="https://www.zhihu.com/people/yczlab" class="tooltipped" target="_blank" data-tooltip="关注我的知乎: https://www.zhihu.com/people/yczlab" data-position="top" data-delay="50">
        <i class="fab fa-zhihu1">知</i>
    </a>



</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    
        <script src="//code.tidio.co/ef3vil8uaqrvp7giqeg545xldzmymqss.js"></script>
    

    

	
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.5'
        zIndex="-1" count="99"
        src="/libs/background/canvas-nest.js"></script>
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="/libs/background/ribbon-refresh.min.js" async="async"></script>
    

    
    <script type="text/javascript" src="/libs/background/ribbon-dynamic.js" async="async"></script>
    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    
	
	<script type="text/javascript">
	var OriginTitile = document.title,st;
	document.addEventListener("visibilitychange", function () {
		document.hidden ? (document.title = "Σ(っ °Д °;)っ我隐藏了！", clearTimeout(st)) : (document.title="(๑•̀ㅂ•́) ✧被发现了！", st=setTimeout(function () {document.title = OriginTitile}, 3e3))
	});
	</script>
	
	<!-- 背景樱花飘落特效 -->
	
	
	<!-- 背景雪花飘落特效 -->
	
	
	<!-- 数字雨特效 -->
	
		<script type="text/javascript">
		//只在桌面版网页启用特效
		var windowWidth = $(window).width();
		if (windowWidth > 768) {
			document.write('<canvas id="digitalrain" width="1440" height="900" style="position:fixed; z-index:-2; right:0px; bottom:0px; min-width:100%; min-height:100%; height:auto; width:auto;"><\/canvas>');
			document.write('<script type="text/javascript" src="/js/digitalrain.js"><\/script>');
		}
		</script>
	

</body>

</html>
